diff --git a/CHANGELOG.md b/CHANGELOG.md index 1328aba5..96906cb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,11 @@ ## [Unreleased] +## [v1.4.3](https://github.com/cosmos/gogoproto/releases/tag/v1.4.2) - 2022-10-14 + ### Bug Fixes +- [#24](https://github.com/cosmos/gogoproto/pull/24) Fix `CompactTextString` panics with nested Anys and private fields. - [#14](https://github.com/cosmos/gogoproto/pull/14) Fix `make regenerate`. ## [v1.4.2](https://github.com/cosmos/gogoproto/releases/tag/v1.4.2) - 2022-09-14 diff --git a/proto/text.go b/proto/text.go index 4bb1a049..75c045e1 100644 --- a/proto/text.go +++ b/proto/text.go @@ -267,9 +267,15 @@ func (tm *TextMarshaler) writeStruct(w *textWriter, sv reflect.Value) error { sprops := GetProperties(st) for i := 0; i < sv.NumField(); i++ { fv := sv.Field(i) + props := sprops.Prop[i] name := st.Field(i).Name + // skip unexported fields (i.e first letter is lowercase) + if name[0] != strings.ToUpper(name)[0] { + continue + } + if name == "XXX_NoUnkeyedLiteral" { continue } @@ -481,6 +487,7 @@ var textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem() // writeAny writes an arbitrary field. func (tm *TextMarshaler) writeAny(w *textWriter, v reflect.Value, props *Properties) error { v = reflect.Indirect(v) + k := v.Kind() if props != nil { if len(props.CustomType) > 0 { @@ -533,7 +540,7 @@ func (tm *TextMarshaler) writeAny(w *textWriter, v reflect.Value, props *Propert } // Floats have special cases. - if v.Kind() == reflect.Float32 || v.Kind() == reflect.Float64 { + if k == reflect.Float32 || k == reflect.Float64 { x := v.Float() var b []byte switch { @@ -553,7 +560,7 @@ func (tm *TextMarshaler) writeAny(w *textWriter, v reflect.Value, props *Propert // We don't attempt to serialise every possible value type; only those // that can occur in protocol buffers. - switch v.Kind() { + switch k { case reflect.Slice: // Should only be a []byte; repeated fields are handled in writeStruct. if err := writeString(w, string(v.Bytes())); err != nil { diff --git a/proto/text_test.go b/proto/text_test.go index dc3abbf7..402c1b48 100644 --- a/proto/text_test.go +++ b/proto/text_test.go @@ -516,3 +516,28 @@ func TestRacyMarshal(t *testing.T) { }() } } + +func TestAny(t *testing.T) { + any := &pb.MyMessage{Count: proto.Int32(47), Name: proto.String("David")} + proto.SetExtension(any, pb.E_Ext_Text, proto.String("bar")) + b, err := proto.Marshal(any) + if err != nil { + panic(err) + } + m := &proto3pb.Message{ + Name: "David", + ResultCount: 47, + Anything: &types.Any{TypeUrl: proto.MessageName(any), Value: b}, + } + + expected := `name: "David" + result_count: 47 + anything: < + type_url: "test_proto.MyMessage" + value: "\302\006\003bar\010/\022\005David" + >` + got := proto.MarshalTextString(m) + if strings.EqualFold(expected, got) { + t.Errorf("got = %s, want %s", expected, got) + } +}