@@ -210,6 +210,75 @@ func TestFieldDescriptionGetFrom(t *testing.T) {
210210 }
211211}
212212
213+ func TestFieldDescriptionGetFromJSON (t * testing.T ) {
214+ UseJSONFieldNames = true
215+ defer func () { UseJSONFieldNames = false }()
216+
217+ pbdb := NewDb ()
218+ msg := & proto3pb.TestAllTypes {
219+ SingleUint64 : 12 ,
220+ SingleDuration : dpb .New (time .Duration (1234 )),
221+ SingleTimestamp : tpb .New (time .Unix (12345 , 0 ).UTC ()),
222+ SingleBoolWrapper : wrapperspb .Bool (false ),
223+ SingleInt32Wrapper : wrapperspb .Int32 (42 ),
224+ StandaloneEnum : proto3pb .TestAllTypes_BAR ,
225+ NestedType : & proto3pb.TestAllTypes_SingleNestedMessage {
226+ SingleNestedMessage : & proto3pb.TestAllTypes_NestedMessage {
227+ Bb : 123 ,
228+ },
229+ },
230+ SingleValue : structpb .NewStringValue ("hello world" ),
231+ SingleStruct : jsonStruct (t , map [string ]any {
232+ "null" : nil ,
233+ }),
234+ }
235+ msgName := string (msg .ProtoReflect ().Descriptor ().FullName ())
236+ _ , err := pbdb .RegisterMessage (msg )
237+ if err != nil {
238+ t .Fatalf ("pbdb.RegisterMessage(%q) failed: %v" , msgName , err )
239+ }
240+ td , found := pbdb .DescribeType (msgName )
241+ if ! found {
242+ t .Fatalf ("pbdb.DescribeType(%q) not found" , msgName )
243+ }
244+ expected := map [string ]any {
245+ "singleUint64" : uint64 (12 ),
246+ "singleDuration" : time .Duration (1234 ),
247+ "singleTimestamp" : time .Unix (12345 , 0 ).UTC (),
248+ "singleBoolWrapper" : false ,
249+ "singleInt32Wrapper" : int32 (42 ),
250+ "singleInt64Wrapper" : structpb .NullValue_NULL_VALUE ,
251+ "singleNestedMessage" : & proto3pb.TestAllTypes_NestedMessage {
252+ Bb : 123 ,
253+ },
254+ "standaloneEnum" : int64 (1 ),
255+ "singleValue" : "hello world" ,
256+ "singleStruct" : jsonStruct (t , map [string ]any {
257+ "null" : nil ,
258+ }),
259+ }
260+ for field , want := range expected {
261+ f , found := td .FieldByName (field )
262+ if ! found {
263+ t .Fatalf ("td.FieldByName(%q) not found" , field )
264+ }
265+ got , err := f .GetFrom (msg )
266+ if err != nil {
267+ t .Fatalf ("field.GetFrom() failed: %v" , err )
268+ }
269+ switch g := got .(type ) {
270+ case proto.Message :
271+ if ! proto .Equal (g , want .(proto.Message )) {
272+ t .Errorf ("got field %s value %v, wanted %v" , field , g , want )
273+ }
274+ default :
275+ if ! reflect .DeepEqual (g , want ) {
276+ t .Errorf ("got field %s value %v, wanted %v" , field , g , want )
277+ }
278+ }
279+ }
280+ }
281+
213282func TestFieldDescriptionIsSet (t * testing.T ) {
214283 pbdb := NewDb ()
215284 msg := & proto3pb.TestAllTypes {}
0 commit comments