Skip to content

Commit ee0de4b

Browse files
authored
Merge pull request #69 from shamaton/fix/v2-format
Format v2 branch files
2 parents 111d580 + 65a5b02 commit ee0de4b

38 files changed

Lines changed: 230 additions & 163 deletions

.github/workflows/test.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
matrix:
1616
os: [ubuntu-latest, macos-latest, windows-latest]
17-
go: ['1.22', '1.23', '1.24']
17+
go: ["1.22", "1.23", "1.24"]
1818
name: ${{ matrix.os }} @ Go ${{ matrix.go }}
1919
runs-on: ${{ matrix.os }}
2020
steps:
@@ -36,7 +36,6 @@ jobs:
3636
restore-keys: |
3737
${{ runner.os }}-go-
3838
39-
4039
- name: Build
4140
run: go build -v ./...
4241

README.md

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fshamaton%2Fmsgpack.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fshamaton%2Fmsgpack?ref=badge_shield)
88

99
## 📣 Announcement: `time.Time` decoding defaults to **UTC** in v3
10+
1011
Starting with **v3.0.0**, when decoding MessagePack **Timestamp** into Go’s `time.Time`,
1112
the default `Location` will be **UTC** (previously `Local`). The instant is unchanged.
1213
To keep the old behavior, use `SetDecodedTimeAsLocal()`.
1314

1415
## Features
16+
1517
* Supported types : primitive / array / slice / struct / map / interface{} and time.Time
1618
* Renaming fields via `msgpack:"field_name"`
1719
* Omitting fields via `msgpack:"-"`
@@ -22,11 +24,13 @@ To keep the old behavior, use `SetDecodedTimeAsLocal()`.
2224
## Installation
2325

2426
Current version is **msgpack/v2**.
27+
2528
```sh
2629
go get -u github.com/shamaton/msgpack/v2
2730
```
2831

2932
## Quick Start
33+
3034
```go
3135
package main
3236

@@ -36,36 +40,36 @@ import (
3640
)
3741

3842
type Struct struct {
39-
String string
43+
String string
4044
}
4145

4246
// simple
4347
func main() {
44-
v := Struct{String: "msgpack"}
45-
46-
d, err := msgpack.Marshal(v)
47-
if err != nil {
48-
panic(err)
49-
}
50-
r := Struct{}
51-
if err = msgpack.Unmarshal(d, &r); err != nil {
52-
panic(err)
53-
}
48+
v := Struct{String: "msgpack"}
49+
50+
d, err := msgpack.Marshal(v)
51+
if err != nil {
52+
panic(err)
53+
}
54+
r := Struct{}
55+
if err = msgpack.Unmarshal(d, &r); err != nil {
56+
panic(err)
57+
}
5458
}
5559

5660
// streaming
5761
func handle(w http.ResponseWriter, r *http.Request) {
58-
var body Struct
59-
if err := msgpack.UnmarshalRead(r, &body); err != nil {
60-
panic(err)
61-
}
62-
if err := msgpack.MarshalWrite(w, body); err != nil {
63-
panic(err)
64-
}
62+
var body Struct
63+
if err := msgpack.UnmarshalRead(r, &body); err != nil {
64+
panic(err)
65+
}
66+
if err := msgpack.MarshalWrite(w, body); err != nil {
67+
panic(err)
68+
}
6569
}
6670
```
6771

68-
## 📣 Announcement: `time.Time` decoding defaults to **UTC** in v3
72+
## v3: `time.Time` decoding defaults to **UTC**
6973

7074
**TL;DR:** Starting with **v3.0.0**, when decoding MessagePack **Timestamp** into Go’s `time.Time`, the default `Location` will be **UTC** (previously `Local`). The **instant** is unchanged—only the display/location changes. This avoids host-dependent differences and aligns with common distributed systems practice.
7175

@@ -111,6 +115,7 @@ msgpack.SetDecodedTimeAsUTC()
111115
```
112116

113117
## Benchmark
118+
114119
This result made from [shamaton/msgpack_bench](https://github.com/shamaton/msgpack_bench)
115120

116121
![msgpack_bench](https://github.com/user-attachments/assets/ed5bc4c5-a149-4083-98b8-ee6820c00eae)

crash_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func check(t *testing.T, wg *sync.WaitGroup, ch <-chan string) {
4545
defer wg.Done()
4646
defer func() {
4747
if e := recover(); e != nil {
48-
t.Logf("panic occurred.\nfile: %s\nlen: %d\nbin: % x\nerr: %+v",
48+
t.Logf(
49+
"panic occurred.\nfile: %s\nlen: %d\nbin: % x\nerr: %+v",
4950
path, len(data), data, e,
5051
)
5152
}
@@ -74,7 +75,8 @@ func check(t *testing.T, wg *sync.WaitGroup, ch <-chan string) {
7475
var r interface{}
7576
err = msgpack.Unmarshal(data, &r)
7677
if err == nil {
77-
t.Logf("err should be occurred.\nname: %s\nlen: %d\nbin: % x",
78+
t.Logf(
79+
"err should be occurred.\nname: %s\nlen: %d\nbin: % x",
7880
file.Name(), len(data), data,
7981
)
8082
t.Fail()

ext/decode.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ type Decoder interface {
2323
}
2424

2525
// DecoderCommon provides common utility methods for decoding data from bytes.
26-
type DecoderCommon struct {
27-
}
26+
type DecoderCommon struct{}
2827

2928
// ReadSize1 reads a single byte from the given index in the byte slice.
3029
// Returns the byte and the new index after reading.

ext/encode.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ type Encoder interface {
2626
// EncoderCommon provides utility methods for encoding various types of values into bytes.
2727
// It includes methods to encode integers and unsigned integers of different sizes,
2828
// as well as methods to write raw byte slices into a target byte slice.
29-
type EncoderCommon struct {
30-
}
29+
type EncoderCommon struct{}
3130

3231
// SetByte1Int64 encodes a single byte from the given int64 value into the byte slice at the specified offset.
3332
// Returns the new offset after writing the byte.

ext/encode_stream.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,25 @@ func CreateStreamWriter(w io.Writer, buf *common.Buffer) StreamWriter {
3232

3333
// WriteByte1Int64 writes a single byte representation of an int64 value.
3434
func (w *StreamWriter) WriteByte1Int64(value int64) error {
35-
return w.buf.Write(w.w,
35+
return w.buf.Write(
36+
w.w,
3637
byte(value),
3738
)
3839
}
3940

4041
// WriteByte2Int64 writes a two-byte representation of an int64 value.
4142
func (w *StreamWriter) WriteByte2Int64(value int64) error {
42-
return w.buf.Write(w.w,
43+
return w.buf.Write(
44+
w.w,
4345
byte(value>>8),
4446
byte(value),
4547
)
4648
}
4749

4850
// WriteByte4Int64 writes a four-byte representation of an int64 value.
4951
func (w *StreamWriter) WriteByte4Int64(value int64) error {
50-
return w.buf.Write(w.w,
52+
return w.buf.Write(
53+
w.w,
5154
byte(value>>24),
5255
byte(value>>16),
5356
byte(value>>8),
@@ -57,7 +60,8 @@ func (w *StreamWriter) WriteByte4Int64(value int64) error {
5760

5861
// WriteByte8Int64 writes an eight-byte representation of an int64 value.
5962
func (w *StreamWriter) WriteByte8Int64(value int64) error {
60-
return w.buf.Write(w.w,
63+
return w.buf.Write(
64+
w.w,
6165
byte(value>>56),
6266
byte(value>>48),
6367
byte(value>>40),
@@ -71,22 +75,25 @@ func (w *StreamWriter) WriteByte8Int64(value int64) error {
7175

7276
// WriteByte1Uint64 writes a single byte representation of a uint64 value.
7377
func (w *StreamWriter) WriteByte1Uint64(value uint64) error {
74-
return w.buf.Write(w.w,
78+
return w.buf.Write(
79+
w.w,
7580
byte(value),
7681
)
7782
}
7883

7984
// WriteByte2Uint64 writes a two-byte representation of a uint64 value.
8085
func (w *StreamWriter) WriteByte2Uint64(value uint64) error {
81-
return w.buf.Write(w.w,
86+
return w.buf.Write(
87+
w.w,
8288
byte(value>>8),
8389
byte(value),
8490
)
8591
}
8692

8793
// WriteByte4Uint64 writes a four-byte representation of a uint64 value.
8894
func (w *StreamWriter) WriteByte4Uint64(value uint64) error {
89-
return w.buf.Write(w.w,
95+
return w.buf.Write(
96+
w.w,
9097
byte(value>>24),
9198
byte(value>>16),
9299
byte(value>>8),
@@ -96,7 +103,8 @@ func (w *StreamWriter) WriteByte4Uint64(value uint64) error {
96103

97104
// WriteByte8Uint64 writes an eight-byte representation of a uint64 value.
98105
func (w *StreamWriter) WriteByte8Uint64(value uint64) error {
99-
return w.buf.Write(w.w,
106+
return w.buf.Write(
107+
w.w,
100108
byte(value>>56),
101109
byte(value>>48),
102110
byte(value>>40),
@@ -110,22 +118,25 @@ func (w *StreamWriter) WriteByte8Uint64(value uint64) error {
110118

111119
// WriteByte1Int writes a single byte representation of an int value.
112120
func (w *StreamWriter) WriteByte1Int(value int) error {
113-
return w.buf.Write(w.w,
121+
return w.buf.Write(
122+
w.w,
114123
byte(value),
115124
)
116125
}
117126

118127
// WriteByte2Int writes a two-byte representation of an int value.
119128
func (w *StreamWriter) WriteByte2Int(value int) error {
120-
return w.buf.Write(w.w,
129+
return w.buf.Write(
130+
w.w,
121131
byte(value>>8),
122132
byte(value),
123133
)
124134
}
125135

126136
// WriteByte4Int writes a four-byte representation of an int value.
127137
func (w *StreamWriter) WriteByte4Int(value int) error {
128-
return w.buf.Write(w.w,
138+
return w.buf.Write(
139+
w.w,
129140
byte(value>>24),
130141
byte(value>>16),
131142
byte(value>>8),
@@ -135,7 +146,8 @@ func (w *StreamWriter) WriteByte4Int(value int) error {
135146

136147
// WriteByte4Uint32 writes a four-byte representation of a uint32 value.
137148
func (w *StreamWriter) WriteByte4Uint32(value uint32) error {
138-
return w.buf.Write(w.w,
149+
return w.buf.Write(
150+
w.w,
139151
byte(value>>24),
140152
byte(value>>16),
141153
byte(value>>8),

internal/common/common.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66
)
77

88
// Common is used encoding/decoding
9-
type Common struct {
10-
}
9+
type Common struct{}
1110

1211
// CheckField returns flag whether should encode/decode or not and field name
1312
func (c *Common) CheckField(field reflect.StructField) (public, omit bool, name string) {

internal/decoding/bool_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package decoding
22

33
import (
4-
"github.com/shamaton/msgpack/v2/def"
54
"reflect"
65
"testing"
6+
7+
"github.com/shamaton/msgpack/v2/def"
78
)
89

910
func Test_asBool(t *testing.T) {

internal/decoding/complex_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ func Test_asComplex64(t *testing.T) {
6262
},
6363
{
6464
Name: "Fixext16.ok",
65-
Data: []byte{def.Fixext16, byte(def.ComplexTypeCode()),
66-
63, 240, 0, 0, 0, 0, 0, 0, 63, 240, 0, 0, 0, 0, 0, 0},
65+
Data: []byte{
66+
def.Fixext16, byte(def.ComplexTypeCode()),
67+
63, 240, 0, 0, 0, 0, 0, 0, 63, 240, 0, 0, 0, 0, 0, 0,
68+
},
6769
Expected: complex(1, 1),
6870
MethodAs: method,
6971
},
@@ -132,8 +134,10 @@ func Test_asComplex128(t *testing.T) {
132134
},
133135
{
134136
Name: "Fixext16.ok",
135-
Data: []byte{def.Fixext16, byte(def.ComplexTypeCode()),
136-
63, 240, 0, 0, 0, 0, 0, 0, 63, 240, 0, 0, 0, 0, 0, 0},
137+
Data: []byte{
138+
def.Fixext16, byte(def.ComplexTypeCode()),
139+
63, 240, 0, 0, 0, 0, 0, 0, 63, 240, 0, 0, 0, 0, 0, 0,
140+
},
137141
Expected: complex(1, 1),
138142
MethodAs: method,
139143
},

internal/decoding/decoding_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ func Test_decodeWithCode(t *testing.T) {
714714
MethodAs: method,
715715
},
716716
}
717-
v := (any)(new(int))
717+
v := any(new(int))
718718
target = &v
719719
testcases.Run(t)
720720
vv := v.(*int)

0 commit comments

Comments
 (0)