@@ -196,14 +196,14 @@ func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeU
196
196
// "Wrappers use the same representation in JSON
197
197
// as the wrapped primitive type, ..."
198
198
sprop := proto .GetProperties (s .Type ())
199
- return m .marshalValue (out , sprop .Prop [1 ], s .Field ( 1 ), indent )
199
+ return m .marshalValue (out , sprop .Prop [1 ], s .FieldByName ( "Value" ), indent )
200
200
case "Any" :
201
201
// Any is a bit more involved.
202
202
return m .marshalAny (out , v , indent )
203
203
case "Duration" :
204
204
// "Generated output always contains 0, 3, 6, or 9 fractional digits,
205
205
// depending on required precision."
206
- s , ns := s .Field ( 1 ).Int (), s .Field ( 2 ).Int ()
206
+ s , ns := s .FieldByName ( "Seconds" ).Int (), s .FieldByName ( "Nanos" ).Int ()
207
207
if ns <= - secondInNanos || ns >= secondInNanos {
208
208
return fmt .Errorf ("ns out of range (%v, %v)" , - secondInNanos , secondInNanos )
209
209
}
@@ -221,14 +221,18 @@ func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeU
221
221
out .write (x )
222
222
out .write (`s"` )
223
223
return out .err
224
- case "Struct" , "ListValue" :
225
- // Let marshalValue handle the `Struct.fields` map or the `ListValue.values` slice.
224
+ case "Struct" :
225
+ // Let marshalValue handle the `Struct.fields` map.
226
+ // TODO: pass the correct Properties if needed.
227
+ return m .marshalValue (out , & proto.Properties {}, s .FieldByName ("Fields" ), indent )
228
+ case "ListValue" :
229
+ // Let marshalValue handle the `ListValue.values` slice.
226
230
// TODO: pass the correct Properties if needed.
227
- return m .marshalValue (out , & proto.Properties {}, s .Field ( 1 ), indent )
231
+ return m .marshalValue (out , & proto.Properties {}, s .FieldByName ( "Values" ), indent )
228
232
case "Timestamp" :
229
233
// "RFC 3339, where generated output will always be Z-normalized
230
234
// and uses 0, 3, 6 or 9 fractional digits."
231
- s , ns := s .Field ( 1 ).Int (), s .Field ( 2 ).Int ()
235
+ s , ns := s .FieldByName ( "Seconds" ).Int (), s .FieldByName ( "Nanos" ).Int ()
232
236
if ns < 0 || ns >= secondInNanos {
233
237
return fmt .Errorf ("ns out of range [0, %v)" , secondInNanos )
234
238
}
@@ -244,7 +248,7 @@ func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeU
244
248
return out .err
245
249
case "Value" :
246
250
// Value has a single oneof.
247
- kind := s .Field ( 1 )
251
+ kind := s .FieldByName ( "Kind" )
248
252
if kind .IsNil () {
249
253
// "absence of any variant indicates an error"
250
254
return errors .New ("nil Value" )
@@ -393,8 +397,8 @@ func (m *Marshaler) marshalAny(out *errWriter, any proto.Message, indent string)
393
397
// Otherwise, the value will be converted into a JSON object,
394
398
// and the "@type" field will be inserted to indicate the actual data type."
395
399
v := reflect .ValueOf (any ).Elem ()
396
- turl := v .Field ( 1 ).String ()
397
- val := v .Field ( 2 ).Bytes ()
400
+ turl := v .FieldByName ( "TypeUrl" ).String ()
401
+ val := v .FieldByName ( "Value" ).Bytes ()
398
402
399
403
var msg proto.Message
400
404
var err error
@@ -723,7 +727,7 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
723
727
switch wellKnownType (target .Addr ().Interface ()) {
724
728
case "DoubleValue" , "FloatValue" , "Int64Value" , "UInt64Value" ,
725
729
"Int32Value" , "UInt32Value" , "BoolValue" , "StringValue" , "BytesValue" :
726
- return u .unmarshalValue (target .Field ( 1 ), inputValue , prop )
730
+ return u .unmarshalValue (target .FieldByName ( "Value" ), inputValue , prop )
727
731
case "Any" :
728
732
// Use json.RawMessage pointer type instead of value to support pre-1.8 version.
729
733
// 1.8 changed RawMessage.MarshalJSON from pointer type to value type, see
@@ -742,7 +746,7 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
742
746
if err := json .Unmarshal ([]byte (* val ), & turl ); err != nil {
743
747
return fmt .Errorf ("can't unmarshal Any's '@type': %q" , * val )
744
748
}
745
- target .Field ( 1 ).SetString (turl )
749
+ target .FieldByName ( "TypeUrl" ).SetString (turl )
746
750
747
751
var m proto.Message
748
752
var err error
@@ -780,7 +784,7 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
780
784
if err != nil {
781
785
return fmt .Errorf ("can't marshal proto %T into Any.Value: %v" , m , err )
782
786
}
783
- target .Field ( 2 ).SetBytes (b )
787
+ target .FieldByName ( "Value" ).SetBytes (b )
784
788
785
789
return nil
786
790
case "Duration" :
@@ -797,8 +801,8 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
797
801
ns := d .Nanoseconds ()
798
802
s := ns / 1e9
799
803
ns %= 1e9
800
- target .Field ( 1 ).SetInt (s )
801
- target .Field ( 2 ).SetInt (ns )
804
+ target .FieldByName ( "Seconds" ).SetInt (s )
805
+ target .FieldByName ( "Nanos" ).SetInt (ns )
802
806
return nil
803
807
case "Timestamp" :
804
808
unq , err := unquote (string (inputValue ))
@@ -811,22 +815,22 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
811
815
return fmt .Errorf ("bad Timestamp: %v" , err )
812
816
}
813
817
814
- target .Field ( 1 ).SetInt (t .Unix ())
815
- target .Field ( 2 ).SetInt (int64 (t .Nanosecond ()))
818
+ target .FieldByName ( "Seconds" ).SetInt (t .Unix ())
819
+ target .FieldByName ( "Nanos" ).SetInt (int64 (t .Nanosecond ()))
816
820
return nil
817
821
case "Struct" :
818
822
var m map [string ]json.RawMessage
819
823
if err := json .Unmarshal (inputValue , & m ); err != nil {
820
824
return fmt .Errorf ("bad StructValue: %v" , err )
821
825
}
822
826
823
- target .Field ( 1 ).Set (reflect .ValueOf (map [string ]* stpb.Value {}))
827
+ target .FieldByName ( "Fields" ).Set (reflect .ValueOf (map [string ]* stpb.Value {}))
824
828
for k , jv := range m {
825
829
pv := & stpb.Value {}
826
830
if err := u .unmarshalValue (reflect .ValueOf (pv ).Elem (), jv , prop ); err != nil {
827
831
return fmt .Errorf ("bad value in StructValue for key %q: %v" , k , err )
828
832
}
829
- target .Field ( 1 ).SetMapIndex (reflect .ValueOf (k ), reflect .ValueOf (pv ))
833
+ target .FieldByName ( "Fields" ).SetMapIndex (reflect .ValueOf (k ), reflect .ValueOf (pv ))
830
834
}
831
835
return nil
832
836
case "ListValue" :
@@ -835,30 +839,30 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
835
839
return fmt .Errorf ("bad ListValue: %v" , err )
836
840
}
837
841
838
- target .Field ( 1 ).Set (reflect .ValueOf (make ([]* stpb.Value , len (s ))))
842
+ target .FieldByName ( "Values" ).Set (reflect .ValueOf (make ([]* stpb.Value , len (s ))))
839
843
for i , sv := range s {
840
- if err := u .unmarshalValue (target .Field ( 1 ).Index (i ), sv , prop ); err != nil {
844
+ if err := u .unmarshalValue (target .FieldByName ( "Values" ).Index (i ), sv , prop ); err != nil {
841
845
return err
842
846
}
843
847
}
844
848
return nil
845
849
case "Value" :
846
850
ivStr := string (inputValue )
847
851
if ivStr == "null" {
848
- target .Field ( 1 ).Set (reflect .ValueOf (& stpb.Value_NullValue {}))
852
+ target .FieldByName ( "Kind" ).Set (reflect .ValueOf (& stpb.Value_NullValue {}))
849
853
} else if v , err := strconv .ParseFloat (ivStr , 0 ); err == nil {
850
- target .Field ( 1 ).Set (reflect .ValueOf (& stpb.Value_NumberValue {v }))
854
+ target .FieldByName ( "Kind" ).Set (reflect .ValueOf (& stpb.Value_NumberValue {v }))
851
855
} else if v , err := unquote (ivStr ); err == nil {
852
- target .Field ( 1 ).Set (reflect .ValueOf (& stpb.Value_StringValue {v }))
856
+ target .FieldByName ( "Kind" ).Set (reflect .ValueOf (& stpb.Value_StringValue {v }))
853
857
} else if v , err := strconv .ParseBool (ivStr ); err == nil {
854
- target .Field ( 1 ).Set (reflect .ValueOf (& stpb.Value_BoolValue {v }))
858
+ target .FieldByName ( "Kind" ).Set (reflect .ValueOf (& stpb.Value_BoolValue {v }))
855
859
} else if err := json .Unmarshal (inputValue , & []json.RawMessage {}); err == nil {
856
860
lv := & stpb.ListValue {}
857
- target .Field ( 1 ).Set (reflect .ValueOf (& stpb.Value_ListValue {lv }))
861
+ target .FieldByName ( "Kind" ).Set (reflect .ValueOf (& stpb.Value_ListValue {lv }))
858
862
return u .unmarshalValue (reflect .ValueOf (lv ).Elem (), inputValue , prop )
859
863
} else if err := json .Unmarshal (inputValue , & map [string ]json.RawMessage {}); err == nil {
860
864
sv := & stpb.Struct {}
861
- target .Field ( 1 ).Set (reflect .ValueOf (& stpb.Value_StructValue {sv }))
865
+ target .FieldByName ( "Kind" ).Set (reflect .ValueOf (& stpb.Value_StructValue {sv }))
862
866
return u .unmarshalValue (reflect .ValueOf (sv ).Elem (), inputValue , prop )
863
867
} else {
864
868
return fmt .Errorf ("unrecognized type for Value %q" , ivStr )
0 commit comments