internal/mcp: unify json tag parsing
Do a thorough job of parsing json struct tags. Move the code to a place where both mcp and jsonschema can use it. Change-Id: I6c86308834706b12a1e22cd96ae120590f49d3f4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/678757 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
b1eb3c8804
commit
12761ffcb6
4 changed files with 101 additions and 97 deletions
|
|
@ -9,8 +9,8 @@ package jsonschema
|
|||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/internal/util"
|
||||
)
|
||||
|
||||
// For constructs a JSON schema object for the given type argument.
|
||||
|
|
@ -108,19 +108,19 @@ func typeSchema(t reflect.Type) (*Schema, error) {
|
|||
|
||||
for i := range t.NumField() {
|
||||
field := t.Field(i)
|
||||
name, required, include := parseField(field)
|
||||
if !include {
|
||||
info := util.FieldJSONInfo(field)
|
||||
if info.Omit {
|
||||
continue
|
||||
}
|
||||
if s.Properties == nil {
|
||||
s.Properties = make(map[string]*Schema)
|
||||
}
|
||||
s.Properties[name], err = typeSchema(field.Type)
|
||||
s.Properties[info.Name], err = typeSchema(field.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if required {
|
||||
s.Required = append(s.Required, name)
|
||||
if !info.Settings["omitempty"] && !info.Settings["omitzero"] {
|
||||
s.Required = append(s.Required, info.Name)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -133,23 +133,3 @@ func typeSchema(t reflect.Type) (*Schema, error) {
|
|||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func parseField(f reflect.StructField) (name string, required, include bool) {
|
||||
if !f.IsExported() {
|
||||
return "", false, false
|
||||
}
|
||||
name = f.Name
|
||||
required = true
|
||||
if tag, ok := f.Tag.Lookup("json"); ok {
|
||||
props := strings.Split(tag, ",")
|
||||
if props[0] != "" {
|
||||
if props[0] == "-" {
|
||||
return "", false, false
|
||||
}
|
||||
name = props[0]
|
||||
}
|
||||
// TODO: support 'omitzero' as well.
|
||||
required = !slices.Contains(props[1:], "omitempty")
|
||||
}
|
||||
return name, required, true
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ import (
|
|||
"reflect"
|
||||
"regexp"
|
||||
"slices"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/internal/util"
|
||||
)
|
||||
|
||||
// A Schema is a JSON schema object.
|
||||
|
|
@ -426,8 +428,9 @@ var (
|
|||
|
||||
func init() {
|
||||
for _, sf := range reflect.VisibleFields(reflect.TypeFor[Schema]()) {
|
||||
if name, ok := jsonName(sf); ok {
|
||||
schemaFieldInfos = append(schemaFieldInfos, structFieldInfo{sf, name})
|
||||
info := util.FieldJSONInfo(sf)
|
||||
if !info.Omit {
|
||||
schemaFieldInfos = append(schemaFieldInfos, structFieldInfo{sf, info.Name})
|
||||
}
|
||||
}
|
||||
slices.SortFunc(schemaFieldInfos, func(i1, i2 structFieldInfo) int {
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ import (
|
|||
"strings"
|
||||
"sync"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/internal/util"
|
||||
)
|
||||
|
||||
// The value of the "$schema" keyword for the version that we can validate.
|
||||
|
|
@ -688,7 +690,8 @@ func properties(v reflect.Value) iter.Seq2[string, reflect.Value] {
|
|||
for name, sf := range structPropertiesOf(v.Type()) {
|
||||
val := v.FieldByIndex(sf.Index)
|
||||
if val.IsZero() {
|
||||
if tag, ok := sf.Tag.Lookup("json"); ok && (strings.Contains(tag, "omitempty") || strings.Contains(tag, "omitzero")) {
|
||||
info := util.FieldJSONInfo(sf)
|
||||
if info.Settings["omitempty"] || info.Settings["omitzero"] {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
|
@ -740,30 +743,11 @@ func structPropertiesOf(t reflect.Type) propertyMap {
|
|||
}
|
||||
props := map[string]reflect.StructField{}
|
||||
for _, sf := range reflect.VisibleFields(t) {
|
||||
if name, ok := jsonName(sf); ok {
|
||||
props[name] = sf
|
||||
info := util.FieldJSONInfo(sf)
|
||||
if !info.Omit {
|
||||
props[info.Name] = sf
|
||||
}
|
||||
}
|
||||
structProperties.Store(t, props)
|
||||
return props
|
||||
}
|
||||
|
||||
// jsonName returns the name for f as would be used by [json.Marshal].
|
||||
// That is the name in the json struct tag, or the field name if there is no tag.
|
||||
// If f is not exported or the tag is "-", jsonName returns "", false.
|
||||
func jsonName(f reflect.StructField) (string, bool) {
|
||||
if !f.IsExported() {
|
||||
return "", false
|
||||
}
|
||||
if tag, ok := f.Tag.Lookup("json"); ok {
|
||||
name, _, found := strings.Cut(tag, ",")
|
||||
// "-" means omit, but "-," means the name is "-"
|
||||
if name == "-" && !found {
|
||||
return "", false
|
||||
}
|
||||
if name != "" {
|
||||
return name, true
|
||||
}
|
||||
}
|
||||
return f.Name, true
|
||||
}
|
||||
|
|
|
|||
|
|
@ -259,26 +259,6 @@ func TestStructInstance(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestJSONName(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 := []string{"A", "B", "", "-", "echo", "foxtrot", ""}
|
||||
tt := reflect.TypeFor[S]()
|
||||
for i := range tt.NumField() {
|
||||
got, _ := jsonName(tt.Field(i))
|
||||
if got != want[i] {
|
||||
t.Errorf("got %q, want %q", got, want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func mustMarshal(x any) json.RawMessage {
|
||||
data, err := json.Marshal(x)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Reference in a new issue