@@ -312,7 +312,9 @@ func assert(cond bool, msg string) {
312312// marshalStructWithMap calls json.Marshal on a value of type T, so T must not
313313// have a MarshalJSON method that calls this function, on pain of infinite regress.
314314//
315- // note that there is a similar function in mcp/util.go, but they are not the same
315+ // Note that there is a similar function in mcp/util.go, but they are not the same.
316+ // Here the function requires `-` json tag, does not clear the mapField map,
317+ // and handles embedded struct due to the implementation of jsonNames in this package.
316318//
317319// TODO: avoid this restriction on T by forcing it to marshal in a default way.
318320// See https://go.dev/play/p/EgXKJHxEx_R.
@@ -357,7 +359,10 @@ func marshalStructWithMap[T any](s *T, mapField string) ([]byte, error) {
357359
358360// unmarshalStructWithMap is the inverse of marshalStructWithMap.
359361// T has the same restrictions as in that function.
360- // note that there is a similar function in mcp/util.go, but they are not the same
362+ //
363+ // Note that there is a similar function in mcp/util.go, but they are not the same.
364+ // Here jsonNames also returns fields from embedded structs, hence this function
365+ // handles embedded structs as well.
361366func unmarshalStructWithMap [T any ](data []byte , v * T , mapField string ) error {
362367 // Unmarshal into the struct, ignoring unknown fields.
363368 if err := json .Unmarshal (data , v ); err != nil {
@@ -383,7 +388,9 @@ var jsonNamesMap sync.Map // from reflect.Type to map[string]bool
383388// jsonNames returns the set of JSON object keys that t will marshal into,
384389// including fields from embedded structs in t.
385390// t must be a struct type.
386- // note that there is a similar function in mcp/util.go, but they are not the same
391+ //
392+ // Note that there is a similar function in mcp/util.go, but they are not the same
393+ // Here the function recurses over embedded structs and includes fields from them.
387394func jsonNames (t reflect.Type ) map [string ]bool {
388395 // Lock not necessary: at worst we'll duplicate work.
389396 if val , ok := jsonNamesMap .Load (t ); ok {
0 commit comments