From 2d548a9b2968000c6c1973ff7f0fcf9a5a0e001c Mon Sep 17 00:00:00 2001 From: Simon Sawert Date: Sun, 1 Oct 2023 23:22:53 +0200 Subject: [PATCH] Don't require hints when converting to schema --- examples/infer_multiple_string_rows.go | 2 +- examples/infer_simple_value.go | 2 +- examples/infer_with_hints.go | 2 +- inferrer.go | 4 ++-- inferrer_test.go | 17 +++++++++++++---- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/examples/infer_multiple_string_rows.go b/examples/infer_multiple_string_rows.go index aace2d3..be9993f 100644 --- a/examples/infer_multiple_string_rows.go +++ b/examples/infer_multiple_string_rows.go @@ -13,7 +13,7 @@ func main() { } schema := jtdinfer. InferStrings(rows, jtdinfer.WithoutHints()). - IntoSchema(jtdinfer.WithoutHints()) + IntoSchema() j, _ := json.MarshalIndent(schema, "", " ") print(string(j)) diff --git a/examples/infer_simple_value.go b/examples/infer_simple_value.go index 127cd70..ef458b3 100644 --- a/examples/infer_simple_value.go +++ b/examples/infer_simple_value.go @@ -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)) diff --git a/examples/infer_with_hints.go b/examples/infer_with_hints.go index d5c0b0a..3377203 100644 --- a/examples/infer_with_hints.go +++ b/examples/infer_with_hints.go @@ -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)) } diff --git a/inferrer.go b/inferrer.go index 2290d3f..e9ec0d7 100644 --- a/inferrer.go +++ b/inferrer.go @@ -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 diff --git a/inferrer_test.go b/inferrer_test.go index 4d08374..6ec0283 100644 --- a/inferrer_test.go +++ b/inferrer_test.go @@ -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) } @@ -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, @@ -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) } @@ -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) }