-
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.
* Implement custom tag support via msgp:tag directive * Remove duplicate tag directive * Add custom tag enc/dec test
- Loading branch information
1 parent
2236701
commit abadd67
Showing
4 changed files
with
123 additions
and
3 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)) | ||
} | ||
} | ||
} |
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