diff --git a/cuesonnet.go b/cuesonnet.go index 38292f174ebe31fcfcff346a6bff93248750812a..6637940925d7b6c00ca817ccdc84cad5e045eee6 100644 --- a/cuesonnet.go +++ b/cuesonnet.go @@ -29,12 +29,19 @@ if err != nil { return fmt.Errorf("could not evaluate jsonnet: %w", err) } - schema := ctx.CompileString(string(sc)) + schema := ctx.CompileString(fmt.Sprintf(` + #_Schema: { + %s + } + data: #_Schema + `, sc)) if schema.Err() != nil { return fmt.Errorf("invalid schema: %w", schema.Err()) } - jsonCUE := ctx.CompileString(jsonData) + jsonCUE := ctx.CompileString(fmt.Sprintf(` + data: %s + `, jsonData)) if jsonCUE.Err() != nil { return fmt.Errorf("invalid JSON: %w", jsonCUE.Err()) } @@ -52,7 +59,8 @@ } return fmt.Errorf("config failed validation with %d errors:\n%s", len(errs), strings.Join(errs, "\n")) } - if err := unified.Decode(&v); err != nil { + resolved := unified.LookupPath(cue.ParsePath("data")) + if err := resolved.Decode(&v); err != nil { return fmt.Errorf("could not decode unified config: %w", err) } diff --git a/cuesonnet_test.go b/cuesonnet_test.go index f645e5e0a14a69db43e7c6781c905d5a6c22d0c2..64025264e4b4203e9abae790ca71cd9669e824c0 100644 --- a/cuesonnet_test.go +++ b/cuesonnet_test.go @@ -19,19 +19,11 @@ //go:embed testdata/fixed.jsonnet fixed string ) -type Schema struct { - FirstName string `json:"firstName"` - LastName string `json:"lastName"` - Age int `json:"age"` - Birthday string `json:"birthday"` - Gopher bool `json:"gopher"` -} - func TestCUEsonnet(t *testing.T) { t.Run("config", func(t *testing.T) { t.Parallel() - var s Schema - err := schema.Decode(strings.NewReader(config), &s) + var m map[string]any + err := schema.Decode(strings.NewReader(config), &m) for _, s := range []string{"2", "firstName", "age"} { if !strings.Contains(err.Error(), s) { t.Logf("did not find %q in error", s) @@ -42,8 +34,8 @@ }) t.Run("almost-fixed", func(t *testing.T) { t.Parallel() - var s Schema - err := schema.Decode(strings.NewReader(almostFixed), &s) + var m map[string]any + err := schema.Decode(strings.NewReader(almostFixed), &m) for _, s := range []string{"1", "LastName"} { if !strings.Contains(err.Error(), s) { t.Logf("did not find %q in error", s) @@ -54,20 +46,9 @@ }) t.Run("fixed", func(t *testing.T) { t.Parallel() - var s Schema - if err := schema.Decode(strings.NewReader(fixed), &s); err != nil { - t.Logf("fixed config should not fail to decode: %v", err) - t.Fail() - } - expect := Schema{ - FirstName: "Jim", - LastName: "Jimbly", - Age: 45, - Birthday: "01/02/2003", - Gopher: true, - } - if s != expect { - t.Logf("did not get expected decoding: %+v != %+v", s, expect) + var m map[string]any + if err := schema.Decode(strings.NewReader(fixed), &m); err != nil { + t.Log("fixed config should not fail to decode") t.Fail() } }) diff --git a/testdata/fixed.jsonnet b/testdata/fixed.jsonnet index 99376df6d2f1fed485213da0a3610bbb67d707c2..0c02f083e5dc8b2e0cab02b66f8f9cbf17720a77 100644 --- a/testdata/fixed.jsonnet +++ b/testdata/fixed.jsonnet @@ -2,5 +2,4 @@ { firstName: 'Jim', lastName: 'Jimbly', age: 45, - birthday: '01/02/2003', } diff --git a/testdata/schema.cue b/testdata/schema.cue index b23b930621669bc096d552ba6cd728f851830175..b54f87ce2720c2bdbe56f695dbc7204e6b1279a8 100644 --- a/testdata/schema.cue +++ b/testdata/schema.cue @@ -1,22 +1,10 @@ -import "time" - -#Schema: { - // Basic schema - firstName: string - lastName: string - age: int - birthday: string - - // Refine as needed - #Title: =~"^[A-Z]" | error("must start with an uppercase letter") - firstName: #Title - lastName: #Title - age: >0 - birthday: time.Format("01/02/2006") +// Basic schema +firstName: string +lastName: string +age: int - // Defaults - gopher: bool | *true -} - -// Apply the schema to root -#Schema +// Refine as needed +#Title: =~"^[A-Z]" | error("must start with an uppercase letter") +firstName: #Title +lastName: #Title +age: >=0