Skip to content

Commit c9b5e20

Browse files
jsonchema: support additional keywords (#69)
1 parent dd5d1ea commit c9b5e20

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

jsonschema/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ type Schema struct {
127127
// https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-00#rfc.section.7
128128
Format string `json:"format,omitempty"`
129129

130-
// Extra allows for additional keywords beyond those specified
130+
// Extra allows for additional keywords beyond those specified.
131131
Extra map[string]any `json:"-"`
132132

133133
// computed fields

jsonschema/util.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
361366
func 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.
387394
func 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

Comments
 (0)