Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect !!str tag for structpb unmarshal #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,12 @@ func (u *unmarshaler) unmarshalScalarBool(node *yaml.Node, value *structpb.Value

// Can be string, bytes, float, or int.
func (u *unmarshaler) unmarshalScalarString(node *yaml.Node, value *structpb.Value) {
if node.Tag == "!!str" {
// Explicitly tagged string.
value.Kind = &structpb.Value_StringValue{StringValue: node.Value}
return
}

floatVal, err := strconv.ParseFloat(node.Value, 64)
if err != nil {
value.Kind = &structpb.Value_StringValue{StringValue: node.Value}
Expand All @@ -1054,7 +1060,7 @@ func (u *unmarshaler) unmarshalScalarString(node *yaml.Node, value *structpb.Val
return
}

// String, float, or int.
// Float or int.
u.unmarshalScalarFloat(node, value, floatVal)
}

Expand Down
2 changes: 2 additions & 0 deletions protoyaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ func TestValue_Dynamic(t *testing.T) {
testValueDynamicRoundTrip(t, "true\n")
testValueDynamicRoundTrip(t, "false\n")
testValueDynamicRoundTrip(t, "1\n")
testValueDynamicRoundTrip(t, "\"1\"\n")
testValueDynamicRoundTrip(t, "foo\n")
testValueDynamicRoundTrip(t, "[]\n")
testValueDynamicRoundTrip(t, "{}\n")
testValueDynamicRoundTrip(t, "foo: bar\n")
testValueDynamicRoundTrip(t, "foo: 1\n")
testValueDynamicRoundTrip(t, "foo: \"1\"\n")
testValueDynamicRoundTrip(t, "foo: true\n")
testValueDynamicRoundTrip(t, "foo: false\n")
testValueDynamicRoundTrip(t, "foo: null\n")
Expand Down
Loading