internal/util: move FieldJSONInfo into jsonschema/util (#251)

This CL effectively removes any dependencies on mcp from jsonschema.
This will enable us to move the repo.
This CL also deletes unused marshalStructWithMap and
unmarshalStructWithMap which had a dependency on FieldJSONInfo.

Fixes: #250
This commit is contained in:
Sam Thanawalla 2025-08-06 12:07:57 -04:00 • committed by GitHub
commit b392875dc5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 82 additions and 264 deletions

View file

@ -8,9 +8,7 @@ import (
"cmp"
"fmt"
"iter"
"reflect"
"slices"
"strings"
)
// Helpers below are copied from gopls' moremaps package.
@ -38,43 +36,6 @@ func KeySlice[M ~map[K]V, K comparable, V any](m M) []K {
return r
}
type JSONInfo struct {
Omit bool // unexported or first tag element is "-"
Name string // Go field name or first tag element. Empty if Omit is true.
Settings map[string]bool // "omitempty", "omitzero", etc.
}
// FieldJSONInfo reports information about how encoding/json
// handles the given struct field.
// If the field is unexported, JSONInfo.Omit is true and no other JSONInfo field
// is populated.
// If the field is exported and has no tag, then Name is the field's name and all
// other fields are false.
// Otherwise, the information is obtained from the tag.
func FieldJSONInfo(f reflect.StructField) JSONInfo {
if !f.IsExported() {
return JSONInfo{Omit: true}
}
info := JSONInfo{Name: f.Name}
if tag, ok := f.Tag.Lookup("json"); ok {
name, rest, found := strings.Cut(tag, ",")
// "-" means omit, but "-," means the name is "-"
if name == "-" && !found {
return JSONInfo{Omit: true}
}
if name != "" {
info.Name = name
}
if len(rest) > 0 {
info.Settings = map[string]bool{}
for _, s := range strings.Split(rest, ",") {
info.Settings[s] = true
}
}
}
return info
}
// Wrapf wraps *errp with the given formatted message if *errp is not nil.
func Wrapf(errp *error, format string, args ...any) {
if *errp != nil {

View file

@ -1,38 +0,0 @@
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package util
import (
"reflect"
"testing"
)
func TestJSONInfo(t *testing.T) {
type S struct {
A int
B int `json:","`
C int `json:"-"`
D int `json:"-,"`
E int `json:"echo"`
F int `json:"foxtrot,omitempty"`
g int `json:"golf"`
}
want := []JSONInfo{
{Name: "A"},
{Name: "B"},
{Omit: true},
{Name: "-"},
{Name: "echo"},
{Name: "foxtrot", Settings: map[string]bool{"omitempty": true}},
{Omit: true},
}
tt := reflect.TypeFor[S]()
for i := range tt.NumField() {
got := FieldJSONInfo(tt.Field(i))
if !reflect.DeepEqual(got, want[i]) {
t.Errorf("got %+v, want %+v", got, want[i])
}
}
}