Skip to content

Commit 5bd02a3

Browse files
jsonschema: copy wrapf into jsonschema/util.go (#254)
jsonschema depends on wrapf in mcp/util.go, copy this into jsonschema for the code move.
1 parent b392875 commit 5bd02a3

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

jsonschema/json_pointer.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import (
2626
"reflect"
2727
"strconv"
2828
"strings"
29-
30-
"github.com/modelcontextprotocol/go-sdk/internal/util"
3129
)
3230

3331
var (
@@ -71,7 +69,7 @@ func parseJSONPointer(ptr string) (segments []string, err error) {
7169
// This implementation suffices for JSON Schema: pointers are applied only to Schemas,
7270
// and refer only to Schemas.
7371
func dereferenceJSONPointer(s *Schema, sptr string) (_ *Schema, err error) {
74-
defer util.Wrapf(&err, "JSON Pointer %q", sptr)
72+
defer wrapf(&err, "JSON Pointer %q", sptr)
7573

7674
segments, err := parseJSONPointer(sptr)
7775
if err != nil {

jsonschema/util.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,10 @@ func fieldJSONInfo(f reflect.StructField) jsonInfo {
454454
}
455455
return info
456456
}
457+
458+
// wrapf wraps *errp with the given formatted message if *errp is not nil.
459+
func wrapf(errp *error, format string, args ...any) {
460+
if *errp != nil {
461+
*errp = fmt.Errorf("%s: %w", fmt.Sprintf(format, args...), *errp)
462+
}
463+
}

jsonschema/validate.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"strings"
1717
"sync"
1818
"unicode/utf8"
19-
20-
"github.com/modelcontextprotocol/go-sdk/internal/util"
2119
)
2220

2321
// The value of the "$schema" keyword for the version that we can validate.
@@ -74,7 +72,7 @@ type state struct {
7472

7573
// validate validates the reflected value of the instance.
7674
func (st *state) validate(instance reflect.Value, schema *Schema, callerAnns *annotations) (err error) {
77-
defer util.Wrapf(&err, "validating %s", st.rs.schemaString(schema))
75+
defer wrapf(&err, "validating %s", st.rs.schemaString(schema))
7876

7977
// Maintain a stack for dynamic schema resolution.
8078
st.stack = append(st.stack, schema) // push
@@ -613,7 +611,7 @@ func (rs *Resolved) ApplyDefaults(instancep any) error {
613611
// Leave this as a potentially recursive helper function, because we'll surely want
614612
// to apply defaults on sub-schemas someday.
615613
func (st *state) applyDefaults(instancep reflect.Value, schema *Schema) (err error) {
616-
defer util.Wrapf(&err, "applyDefaults: schema %s, instance %v", st.rs.schemaString(schema), instancep)
614+
defer wrapf(&err, "applyDefaults: schema %s, instance %v", st.rs.schemaString(schema), instancep)
617615

618616
schemaInfo := st.rs.resolvedInfos[schema]
619617
instance := instancep.Elem()

0 commit comments

Comments
 (0)