-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14c387c
commit 4f514b9
Showing
2 changed files
with
73 additions
and
0 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
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)) | ||
} | ||
} | ||
} |