Home

cuesonnet @main - refs - log -
-
https://git.jolheiser.com/cuesonnet.git
CUE + Jsonnet
tree log patch
remove nested schema Signed-off-by: jolheiser <git@jolheiser.com>
Signature
-----BEGIN SSH SIGNATURE----- U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgBTEvCQk6VqUAdN2RuH6bj1dNkY oOpbPWj+jw4ua1B1cAAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5 AAAAQGe4NvBnb5E6ns82CEQccp9OKHEFBR6acj3KLiOHgCUNm6yNKMw8zS8agPCEvSpPaD 2WzMKLG63kRBdveb3KeQQ= -----END SSH SIGNATURE-----
jolheiser <git@jolheiser.com>
2 weeks ago
4 changed files, 51 additions(+), 27 deletions(-)
cuesonnet.gocuesonnet_test.gotestdata/fixed.jsonnettestdata/schema.cue
M cuesonnet.gocuesonnet.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
diff --git a/cuesonnet.go b/cuesonnet.go
index 6637940925d7b6c00ca817ccdc84cad5e045eee6..38292f174ebe31fcfcff346a6bff93248750812a 100644
--- a/cuesonnet.go
+++ b/cuesonnet.go
@@ -29,19 +29,12 @@ 	if err != nil {
 		return fmt.Errorf("could not evaluate jsonnet: %w", err)
 	}
 
-	schema := ctx.CompileString(fmt.Sprintf(`
-			#_Schema: {
-				%s
-			}
-			data: #_Schema
-		`, sc))
+	schema := ctx.CompileString(string(sc))
 	if schema.Err() != nil {
 		return fmt.Errorf("invalid schema: %w", schema.Err())
 	}
 
-	jsonCUE := ctx.CompileString(fmt.Sprintf(`
-			data: %s
-		`, jsonData))
+	jsonCUE := ctx.CompileString(jsonData)
 	if jsonCUE.Err() != nil {
 		return fmt.Errorf("invalid JSON: %w", jsonCUE.Err())
 	}
@@ -59,8 +52,7 @@ 		}
 		return fmt.Errorf("config failed validation with %d errors:\n%s", len(errs), strings.Join(errs, "\n"))
 	}
 
-	resolved := unified.LookupPath(cue.ParsePath("data"))
-	if err := resolved.Decode(&v); err != nil {
+	if err := unified.Decode(&v); err != nil {
 		return fmt.Errorf("could not decode unified config: %w", err)
 	}
 
M cuesonnet_test.gocuesonnet_test.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
diff --git a/cuesonnet_test.go b/cuesonnet_test.go
index 64025264e4b4203e9abae790ca71cd9669e824c0..f645e5e0a14a69db43e7c6781c905d5a6c22d0c2 100644
--- a/cuesonnet_test.go
+++ b/cuesonnet_test.go
@@ -19,11 +19,19 @@ 	//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 m map[string]any
-		err := schema.Decode(strings.NewReader(config), &m)
+		var s Schema
+		err := schema.Decode(strings.NewReader(config), &s)
 		for _, s := range []string{"2", "firstName", "age"} {
 			if !strings.Contains(err.Error(), s) {
 				t.Logf("did not find %q in error", s)
@@ -34,8 +42,8 @@ 	})
 
 	t.Run("almost-fixed", func(t *testing.T) {
 		t.Parallel()
-		var m map[string]any
-		err := schema.Decode(strings.NewReader(almostFixed), &m)
+		var s Schema
+		err := schema.Decode(strings.NewReader(almostFixed), &s)
 		for _, s := range []string{"1", "LastName"} {
 			if !strings.Contains(err.Error(), s) {
 				t.Logf("did not find %q in error", s)
@@ -46,9 +54,20 @@ 	})
 
 	t.Run("fixed", func(t *testing.T) {
 		t.Parallel()
-		var m map[string]any
-		if err := schema.Decode(strings.NewReader(fixed), &m); err != nil {
-			t.Log("fixed config should not fail to decode")
+		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)
 			t.Fail()
 		}
 	})
M testdata/fixed.jsonnettestdata/fixed.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
diff --git a/testdata/fixed.jsonnet b/testdata/fixed.jsonnet
index 0c02f083e5dc8b2e0cab02b66f8f9cbf17720a77..99376df6d2f1fed485213da0a3610bbb67d707c2 100644
--- a/testdata/fixed.jsonnet
+++ b/testdata/fixed.jsonnet
@@ -2,4 +2,5 @@ {
   firstName: 'Jim',
   lastName: 'Jimbly',
   age: 45,
+  birthday: '01/02/2003',
 }
M testdata/schema.cuetestdata/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
diff --git a/testdata/schema.cue b/testdata/schema.cue
index b54f87ce2720c2bdbe56f695dbc7204e6b1279a8..b23b930621669bc096d552ba6cd728f851830175 100644
--- a/testdata/schema.cue
+++ b/testdata/schema.cue
@@ -1,10 +1,22 @@
-// Basic schema
-firstName: string
-lastName: string
-age:       int
+import "time"
 
-// Refine as needed
-#Title:    =~"^[A-Z]" | error("must start with an uppercase letter")
-firstName: #Title
-lastName:  #Title
-age:       >=0
+#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")
+
+	// Defaults
+	gopher: bool | *true
+}
+
+// Apply the schema to root
+#Schema