Home

cuesonnet @main - refs - log -
-
https://git.jolheiser.com/cuesonnet.git
CUE + Jsonnet
tree log patch
add list test and CLI test Signed-off-by: jolheiser <git@jolheiser.com>
Signature
-----BEGIN SSH SIGNATURE----- U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgBTEvCQk6VqUAdN2RuH6bj1dNkY oOpbPWj+jw4ua1B1cAAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5 AAAAQLuVSG6M23SK2yqVs6FlqJGmYrn/6/Fvr+gyglweSyn5YJn/Qj3gUtY/IILyFOgSbI VESIZcw/AtYio20eKjrgU= -----END SSH SIGNATURE-----
jolheiser <git@jolheiser.com>
1 day ago
5 changed files, 99 additions(+), 1 deletions(-)
cmd/cuesonnet/main.gocmd/cuesonnet/main_test.gocuesonnet_test.gotestdata/kitchen_list.jsonnettestdata/schema_list.cue
M cmd/cuesonnet/main.go -> cmd/cuesonnet/main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
diff --git a/cmd/cuesonnet/main.go b/cmd/cuesonnet/main.go
index 190f6585488e36cc8f782f0af4c6fb69480193ac..656baf09ddc2ca6e9391f1e4e5187952ecfb7c33 100644
--- a/cmd/cuesonnet/main.go
+++ b/cmd/cuesonnet/main.go
@@ -25,6 +25,10 @@ 	if err != nil {
 		return fmt.Errorf("could not open data: %w", err)
 	}
 
+	return cli(schema, data)
+}
+
+func cli(schema, data []byte) error {
 	var a any
 	return cuesonnet.Schema(string(schema)).Decode(bytes.NewReader(data), &a)
 }
I cmd/cuesonnet/main_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
diff --git a/cmd/cuesonnet/main_test.go b/cmd/cuesonnet/main_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..a63dbae04009809853cc0755a710baed3f7c0cd4
--- /dev/null
+++ b/cmd/cuesonnet/main_test.go
@@ -0,0 +1,44 @@
+package main
+
+import "testing"
+
+func TestCLI(t *testing.T) {
+	schema := `import "time"
+#Schema: {
+	firstName: string
+	lastName:  string
+	age:       int
+	birthday:  string
+	#Title:    =~"^[A-Z]" | error("must start with an uppercase letter")
+	firstName: #Title
+	lastName:  #Title
+	age:       >0
+	birthday:  time.Format("01/02/2006")
+	gopher: bool | *true
+}
+`
+	data := `local name = 'Jim';
+local age = 45;
+
+local userData = function(name, age) {
+  firstName: name,
+  lastName: name + 'bly',
+  age: age,
+  birthday: '01/02/2003',
+};
+`
+
+	schemaSingle := schema + "#Schema"
+	dataSingle := data + "userData(name, age)"
+	if err := cli([]byte(schemaSingle), []byte(dataSingle)); err != nil {
+		t.Logf("single schema should match single data: %q", err)
+		t.FailNow()
+	}
+
+	schemaList := schema + "[...#Schema]"
+	dataList := data + "[userData(name, age)]"
+	if err := cli([]byte(schemaList), []byte(dataList)); err != nil {
+		t.Logf("list schema should match list data: %q", err)
+		t.FailNow()
+	}
+}
M cuesonnet_test.go -> cuesonnet_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
diff --git a/cuesonnet_test.go b/cuesonnet_test.go
index e867a77ca895f2f6e81a97c2e76e5bb86b7df6e3..8f52a14d70c2e021ca7763fc7ada029882651bb9 100644
--- a/cuesonnet_test.go
+++ b/cuesonnet_test.go
@@ -11,6 +11,8 @@
 var (
 	//go:embed testdata/schema.cue
 	schema cuesonnet.Schema
+	//go:embed testdata/schema_list.cue
+	schemaList cuesonnet.Schema
 	//go:embed testdata/config.jsonnet
 	config string
 	//go:embed testdata/almost-fixed.jsonnet
@@ -19,6 +21,8 @@ 	//go:embed testdata/fixed.jsonnet
 	fixed string
 	//go:embed testdata/kitchen.jsonnet
 	kitchen string
+	//go:embed testdata/kitchen_list.jsonnet
+	kitchenList string
 )
 
 type Schema struct {
@@ -48,7 +52,7 @@ 		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)
+				t.Logf("did not find %q in error: %q", s, err)
 				t.Fail()
 			}
 		}
@@ -81,6 +85,19 @@ 			t.Logf("kitchen config should not fail to decode: %v", err)
 			t.Fail()
 		}
 		if s != expect {
+			t.Logf("did not get expected decoding: %+v != %+v", s, expect)
+			t.Fail()
+		}
+	})
+
+	t.Run("list", func(t *testing.T) {
+		t.Parallel()
+		var s []Schema
+		if err := schemaList.Decode(strings.NewReader(kitchenList), &s); err != nil {
+			t.Logf("kitchen_list config should not fail to decode: %v", err)
+			t.Fail()
+		}
+		if s[0] != expect {
 			t.Logf("did not get expected decoding: %+v != %+v", s, expect)
 			t.Fail()
 		}
I testdata/kitchen_list.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
diff --git a/testdata/kitchen_list.jsonnet b/testdata/kitchen_list.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..219bb98d348fcef47c974ea4dd36e4ec022c6b8e
--- /dev/null
+++ b/testdata/kitchen_list.jsonnet
@@ -0,0 +1,11 @@
+local name = 'Jim';
+local age = 45;
+
+local userData = function(name, age) {
+  firstName: name,
+  lastName: name + 'bly',
+  age: age,
+  birthday: '01/02/2003',
+};
+
+[userData(name, age)]
I testdata/schema_list.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
diff --git a/testdata/schema_list.cue b/testdata/schema_list.cue
new file mode 100644
index 0000000000000000000000000000000000000000..02024313163d96024bd64c4f01ef2dbd7e83b788
--- /dev/null
+++ b/testdata/schema_list.cue
@@ -0,0 +1,22 @@
+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")
+
+	// Defaults
+	gopher: bool | *true
+}
+
+// Apply the schema to root
+[...#Schema]