Skip to content

Commit

Permalink
Don't require hints when converting to schema
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed Oct 1, 2023
1 parent 247ce1f commit 2d548a9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/infer_multiple_string_rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
}
schema := jtdinfer.
InferStrings(rows, jtdinfer.WithoutHints()).
IntoSchema(jtdinfer.WithoutHints())
IntoSchema()

j, _ := json.MarshalIndent(schema, "", " ")
print(string(j))
Expand Down
2 changes: 1 addition & 1 deletion examples/infer_simple_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func main() {
schema := jtdinfer.
NewInferrer(jtdinfer.WithoutHints()).
Infer("my-string").
IntoSchema(jtdinfer.WithoutHints())
IntoSchema()

j, _ := json.MarshalIndent(schema, "", " ")
print(string(j))
Expand Down
2 changes: 1 addition & 1 deletion examples/infer_with_hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
Discriminator: jtdinfer.NewHintSet().Add([]string{"discriminator", "-", "type"}),
}

schema := jtdinfer.InferStrings(rows, hints).IntoSchema(hints)
schema := jtdinfer.InferStrings(rows, hints).IntoSchema()
j, _ := json.MarshalIndent(schema, "", " ")
print(string(j))
}
4 changes: 2 additions & 2 deletions inferrer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func (i *Inferrer) Infer(value any) *Inferrer {
}

// IntoSchema will convert the `InferredSchema` into a final `Schema`.
func (i *Inferrer) IntoSchema(hints Hints) Schema {
return i.Inference.IntoSchema(hints)
func (i *Inferrer) IntoSchema() Schema {
return i.Inference.IntoSchema(i.Hints)
}

// InferStrings accepts a slice of strings and will convert them to either a
Expand Down
17 changes: 13 additions & 4 deletions inferrer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestJTDInfer(t *testing.T) {
"hobbies": {Elements: &Schema{Type: jtd.TypeString}},
},
}
gotSchema := InferStrings(rows, Hints{}).IntoSchema(Hints{})
gotSchema := InferStrings(rows, WithoutHints()).IntoSchema()

assert.EqualValues(t, expectedSchema, gotSchema)
}
Expand Down Expand Up @@ -48,10 +48,19 @@ func TestJTDInferrerWithEnumHints(t *testing.T) {
},
},
}
gotSchema := InferStrings(rows, hints).IntoSchema(hints)
gotSchema := InferStrings(rows, hints).IntoSchema()

// We check that we got the same elements in our enum first and then we
// delete it since the order is unreliable due to being a map.
require.ElementsMatch(
t,
expectedSchema.Properties["name"].Enum,
gotSchema.Properties["name"].Enum,
)

delete(expectedSchema.Properties, "name")
delete(gotSchema.Properties, "name")

require.ElementsMatch(
t,
expectedSchema.Properties["address"].Properties["city"].Enum,
Expand Down Expand Up @@ -81,7 +90,7 @@ func TestJTDInferWithValuesHints(t *testing.T) {
},
},
}
gotSchema := InferStrings(rows, hints).IntoSchema(hints)
gotSchema := InferStrings(rows, hints).IntoSchema()

assert.EqualValues(t, expectedSchema, gotSchema)
}
Expand Down Expand Up @@ -112,7 +121,7 @@ func TestJTDInferWithDiscriminatorHints(t *testing.T) {
},
},
}
gotSchema := InferStrings(rows, hints).IntoSchema(hints)
gotSchema := InferStrings(rows, hints).IntoSchema()

assert.EqualValues(t, expectedSchema, gotSchema)
}

0 comments on commit 2d548a9

Please sign in to comment.