Skip to content

Commit 2a1c0a4

Browse files
authored
jsonschema: change TypeSchemas key to reflect.Type (#29)
BREAKING CHANGE The key of ForOptions.TypeSchemas cannot be a value of the type, because some types can't be map keys. So change it to reflect.Type. Fixes #26.
1 parent e08864c commit 2a1c0a4

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

jsonschema/infer.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ type ForOptions struct {
2626
IgnoreInvalidTypes bool
2727

2828
// TypeSchemas maps types to their schemas.
29-
// If [For] encounters a type equal to a type of a key in this map, the
29+
// If [For] encounters a type that is a key in this map, the
3030
// corresponding value is used as the resulting schema (after cloning to
3131
// ensure uniqueness).
3232
// Types in this map override the default translations, as described
3333
// in [For]'s documentation.
34-
TypeSchemas map[any]*Schema
34+
TypeSchemas map[reflect.Type]*Schema
3535
}
3636

3737
// For constructs a JSON schema object for the given type argument.
@@ -78,9 +78,7 @@ func For[T any](opts *ForOptions) (*Schema, error) {
7878
}
7979
schemas := maps.Clone(initialSchemaMap)
8080
// Add types from the options. They override the default ones.
81-
for v, s := range opts.TypeSchemas {
82-
schemas[reflect.TypeOf(v)] = s
83-
}
81+
maps.Copy(schemas, opts.TypeSchemas)
8482
s, err := forType(reflect.TypeFor[T](), map[reflect.Type]bool{}, opts.IgnoreInvalidTypes, schemas)
8583
if err != nil {
8684
var z T
@@ -93,9 +91,7 @@ func For[T any](opts *ForOptions) (*Schema, error) {
9391
func ForType(t reflect.Type, opts *ForOptions) (*Schema, error) {
9492
schemas := maps.Clone(initialSchemaMap)
9593
// Add types from the options. They override the default ones.
96-
for v, s := range opts.TypeSchemas {
97-
schemas[reflect.TypeOf(v)] = s
98-
}
94+
maps.Copy(schemas, opts.TypeSchemas)
9995
s, err := forType(t, map[reflect.Type]bool{}, opts.IgnoreInvalidTypes, schemas)
10096
if err != nil {
10197
return nil, fmt.Errorf("ForType(%s): %w", t, err)

jsonschema/infer_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func forType[T any](ignore bool) *jsonschema.Schema {
2525

2626
opts := &jsonschema.ForOptions{
2727
IgnoreInvalidTypes: ignore,
28-
TypeSchemas: map[any]*jsonschema.Schema{
29-
custom(0): {Type: "custom"},
28+
TypeSchemas: map[reflect.Type]*jsonschema.Schema{
29+
reflect.TypeFor[custom](): {Type: "custom"},
3030
},
3131
}
3232
s, err = jsonschema.For[T](opts)
@@ -178,8 +178,8 @@ func TestForType(t *testing.T) {
178178
// ForType is virtually identical to For. Just test that options are handled properly.
179179
opts := &jsonschema.ForOptions{
180180
IgnoreInvalidTypes: true,
181-
TypeSchemas: map[any]*jsonschema.Schema{
182-
custom(0): {Type: "custom"},
181+
TypeSchemas: map[reflect.Type]*jsonschema.Schema{
182+
reflect.TypeFor[custom](): {Type: "custom"},
183183
},
184184
}
185185

0 commit comments

Comments
 (0)