-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
20 changed files
with
481 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package _generated | ||
|
||
//go:generate msgp | ||
//msgp:tag mytag | ||
|
||
type CustomTag struct { | ||
Foo string `mytag:"foo_custom_name"` | ||
Bar int `mytag:"bar1234"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package _generated | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"bytes" | ||
|
||
"github.com/tinylib/msgp/msgp" | ||
) | ||
|
||
func TestCustomTag(t *testing.T) { | ||
t.Run("File Scope", func(t *testing.T) { | ||
ts := CustomTag{ | ||
Foo: "foostring13579", | ||
Bar: 999_999} | ||
encDecCustomTag(t, ts, "mytag") | ||
}) | ||
} | ||
|
||
func encDecCustomTag(t *testing.T, testStruct msgp.Encodable, tag string) { | ||
var b bytes.Buffer | ||
msgp.Encode(&b, testStruct) | ||
|
||
// Check tag names using JSON as an intermediary layer | ||
// TODO: is there a way to avoid the JSON layer? We'd need to directly decode raw msgpack -> map[string]any | ||
refJSON, err := json.Marshal(testStruct) | ||
if err != nil { | ||
t.Error(fmt.Sprintf("error encoding struct as JSON: %v", err)) | ||
} | ||
ref := make(map[string]any) | ||
// Encoding and decoding the original struct via JSON is necessary | ||
// for field comparisons to work, since JSON -> map[string]any | ||
// relies on type inferences such as all numbers being float64s | ||
json.Unmarshal(refJSON, &ref) | ||
|
||
var encJSON bytes.Buffer | ||
msgp.UnmarshalAsJSON(&encJSON, b.Bytes()) | ||
encoded := make(map[string]any) | ||
json.Unmarshal(encJSON.Bytes(), &encoded) | ||
|
||
tsType := reflect.TypeOf(testStruct) | ||
for i := 0; i < tsType.NumField(); i++ { | ||
// Check encoded field name | ||
field := tsType.Field(i) | ||
encodedValue, ok := encoded[field.Tag.Get(tag)] | ||
if !ok { | ||
t.Error("missing encoded value for field", field.Name) | ||
continue | ||
} | ||
// Check encoded field value (against original value post-JSON enc + dec) | ||
jsonName, ok := field.Tag.Lookup("json") | ||
if !ok { | ||
jsonName = field.Name | ||
} | ||
refValue := ref[jsonName] | ||
if !reflect.DeepEqual(refValue, encodedValue) { | ||
t.Error(fmt.Sprintf("incorrect encoded value for field %s. reference: %v, encoded: %v", | ||
field.Name, refValue, encodedValue)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
module github.com/tinylib/msgp | ||
|
||
go 1.18 | ||
go 1.20 | ||
|
||
require ( | ||
github.com/philhofer/fwd v1.1.2 | ||
golang.org/x/tools v0.14.0 | ||
github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986 | ||
golang.org/x/tools v0.22.0 | ||
) | ||
|
||
require ( | ||
golang.org/x/mod v0.13.0 // indirect | ||
golang.org/x/sys v0.13.0 // indirect | ||
) | ||
require golang.org/x/mod v0.18.0 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw= | ||
github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= | ||
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY= | ||
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= | ||
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= | ||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc= | ||
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg= | ||
github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986 h1:jYi87L8j62qkXzaYHAQAhEapgukhenIMZRBKTNRLHJ4= | ||
github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM= | ||
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0= | ||
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= | ||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= | ||
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA= | ||
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.