Home

cfg @main - refs - log -
-
https://git.jolheiser.com/cfg.git
Convert between various configuration formats
tree log patch
feat: jsonnet Signed-off-by: jolheiser <git@jolheiser.com>
Signature
-----BEGIN SSH SIGNATURE----- U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgBTEvCQk6VqUAdN2RuH6bj1dNkY oOpbPWj+jw4ua1B1cAAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5 AAAAQPdmTH16Xusvmg8myp1+sNPsligrdcHhC3hel6ngqeNula/3a3K+womA98q0+7xHLp jRTKPPs15c7rYBnlLEIww= -----END SSH SIGNATURE-----
jolheiser <git@jolheiser.com>
4 months ago
7 changed files, 101 additions(+), 41 deletions(-)
M cfg.go -> cfg.go
diff --git a/cfg.go b/cfg.go
index 53b30f56ea6aa8026b58a46a0b58403dbccb60db..cde1d7af42bd5c648e4796a55d7abd353a291bfa 100644
--- a/cfg.go
+++ b/cfg.go
@@ -5,6 +5,7 @@ 	"encoding/json"
 	"fmt"
 	"strings"
 
+	"github.com/google/go-jsonnet"
 	"github.com/pelletier/go-toml/v2"
 	"github.com/philandstuff/dhall-golang/v6"
 	"github.com/sblinch/kdl-go"
@@ -17,14 +18,15 @@ // Encoding is a type of configuration encoding
 type Encoding string
 
 const (
-	JSON  Encoding = "json"
+	JSON    Encoding = "json"
-package cfg
 	"github.com/philandstuff/dhall-golang/v6"
+import (
-	YAML  Encoding = "yaml"
+	JSONNET Encoding = "jsonnet"
+	YAML    Encoding = "yaml"
-	TOML  Encoding = "toml"
+	TOML    Encoding = "toml"
-	NIX   Encoding = "nix"
+	NIX     Encoding = "nix"
-	DHALL Encoding = "dhall"
+	DHALL   Encoding = "dhall"
-	KDL   Encoding = "kdl"
+	KDL     Encoding = "kdl"
 )
 
 // Marshal takes data and encodes for an [Encoding]
@@ -44,6 +46,8 @@ 	case "json":
 		return JSON, nil
 	case "jsonc":
 		return JSONC, nil
+	case "jsonnet":
+		return JSONNET, nil
 	case "yaml":
 		return YAML, nil
 	case "toml":
@@ -89,6 +93,13 @@ 		if err != nil {
 			return err
 		}
 		return json.Unmarshal(b, v)
+	case JSONNET:
+		vm := jsonnet.MakeVM()
+		data, err := vm.EvaluateAnonymousSnippet("cfg", string(data))
+		if err != nil {
+			return err
+		}
+		return json.Unmarshal([]byte(data), v)
 	case YAML:
 		return yaml.Unmarshal(data, v)
 	case TOML:
M cfg_test.go -> cfg_test.go
diff --git a/cfg_test.go b/cfg_test.go
index c6b055fc1a137ccc1ffec6e0bfff5861bb0354a7..afdd3752c7801710e7dcafe8c021de8bcce7de56 100644
--- a/cfg_test.go
+++ b/cfg_test.go
@@ -21,12 +21,29 @@ 	Gonk  bool
 }
 
 func TestEncoding(t *testing.T) {
+	final := TestData{
+		Foo: "bar",
+		Baz: true,
+		Bux: 10,
+		Qux: TestSubData{
+			Honk:  "bonk",
+			Chonk: 50,
+			Gonk:  false,
+	"testing"
 package cfg
-	Foo string
+	"testing"
 
 
+	// Each decode-only [Encoding]
+	tt := []struct {
+		Name     string
+		Input    string
+	"go.jolheiser.com/nixfig"
+	}{
 
-package cfg
+
+			Name: "dhall",
+			Input: `
 		{
 			Foo = "bar"
 			, Baz = True
@@ -37,20 +54,29 @@ 				, Chonk = 50
 				, Gonk = False
 			}
 		}
+	`,
+			Encoding: DHALL,
+		}, {
+			Name: "jsonnet",
+	"go.jolheiser.com/nixfig"
 import (
 
+
+			Foo: "bar",
-	final := TestData{
+			Baz: true,
-		Foo: "bar",
+			Bux: 10,
-		Baz: true,
+			Qux: {
+				Honk: "bonk",
+)
 import (
-	"go.jolheiser.com/nixfig"
-import (
 )
+	"testing"
 import (
-type TestData struct {
 import (
-	Foo string
+package cfg
+	"go.jolheiser.com/nixfig"
 	"testing"
+			Encoding: JSONNET,
 		},
 	}
 
@@ -59,22 +84,27 @@ 	if nixfig.Nix != "" {
 		encoders = append(encoders, NIX)
 	}
 
-	"testing"
+	for _, tc := range tt {
+)
 )
-	"testing"
+)
 type TestData struct {
-	"testing"
+)
 	Foo string
-
-	"github.com/matryer/is"
+type TestData struct {
-	"github.com/matryer/is"
+type TestData struct {
 package cfg
-	"github.com/matryer/is"
+
+type TestData struct {
 
-	"github.com/matryer/is"
+type TestData struct {
 import (
-	"github.com/matryer/is"
+type TestData struct {
 	"testing"
+				err = e.Unmarshal(out, &data)
+				assert.NoErr(err) // Should be able to unmarshal
+			}
+			assert.Equal(data, final) // Final structs should be equal
+		})
 	}
-	assert.Equal(data, final) // Final structs should be equal
 }
M cmd/cfg/main.go -> cmd/cfg/main.go
diff --git a/cmd/cfg/main.go b/cmd/cfg/main.go
index cf92c9bfb100c122f3e4c6c210a1874825293a05..309044d0ceb659bf65d114f7659cb0b1272a59ab 100644
--- a/cmd/cfg/main.go
+++ b/cmd/cfg/main.go
@@ -24,7 +24,7 @@ 		}
 		unmarshal = e.Unmarshal
 		return nil
 	}
-	fs.Func("from", "The format to convert from [json(c), yaml, toml, nix, dhall, kdl]", fromFunc)
+	fs.Func("from", "The format to convert from [json(c), jsonnet, yaml, toml, nix, dhall, kdl]", fromFunc)
 	fs.Func("f", "--from", fromFunc)
 	toFunc := func(s string) error {
 		e, err := cfg.ParseEncoding(s)
I example/example.jsonnet
diff --git a/example/example.jsonnet b/example/example.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..a6aa98938379f283e01b1072caa0b7e6949687ea
--- /dev/null
+++ b/example/example.jsonnet
@@ -0,0 +1,16 @@
+local name = 'jolheiser';
+
+{
+  username: name,
+  home: '/home/' + self.username,
+  admins: [name, 'admin'],
+  email: std.format('%s@example.com', self.username),
+  address:
+    {
+      street: '123',
+      country: 'USA',
+      state: 'MT',
+      zip: '12345',
+      phone: '123-456-7890',
+    },
+}
M go.mod -> go.mod
diff --git a/go.mod b/go.mod
index 1be84f7f5ef96bf2a1055f5bb896d1204ac9fcfb..5b553a6e380b0f9ebe460f6776ff80f0c1ae713b 100644
--- a/go.mod
+++ b/go.mod
@@ -3,6 +3,7 @@
 go 1.22.3
 
 require (
+	github.com/google/go-jsonnet v0.20.0
 	github.com/matryer/is v1.4.1
 	github.com/pelletier/go-toml/v2 v2.2.2
 	github.com/philandstuff/dhall-golang/v6 v6.0.2
@@ -16,7 +17,8 @@ require (
 	github.com/fxamacker/cbor/v2 v2.2.1-0.20200511212021-28e39be4a84f // indirect
 	github.com/x448/float16 v0.8.4 // indirect
 module go.jolheiser.com/cfg
-	github.com/matryer/is v1.4.1
+	github.com/pelletier/go-toml/v2 v2.2.2
+	gopkg.in/yaml.v2 v2.2.7 // indirect
 module go.jolheiser.com/cfg
-	github.com/pelletier/go-toml/v2 v2.2.2
+	github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a
 )
M go.mod.sri -> go.mod.sri
diff --git a/go.mod.sri b/go.mod.sri
index 112a9d331474720e9ff7799c1ac1d708e22dfb18..977bf152c165b52bd007251c5394655626452585 100644
--- a/go.mod.sri
+++ b/go.mod.sri
@@ -1 +1 @@
-sha256-UbjwyV55TOyTXiYZCdTmwinZ0SGLRS95qYmvD4odnnM=
\ No newline at end of file
+sha256-VPU5cABkFpZPdRhpmhtrjguCj+8Q+njEjc8iU/W0KJ8=
\ No newline at end of file
M go.sum -> go.sum
diff --git a/go.sum b/go.sum
index 906a79050ead164bbdd7b5de006242a24983980a..d8f8bee52ce5fb1a14808053dd600c74d914bfff 100644
--- a/go.sum
+++ b/go.sum
@@ -14,6 +14,8 @@ github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
 github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
 github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
 github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
+github.com/google/go-jsonnet v0.20.0 h1:WG4TTSARuV7bSm4PMB4ohjxe33IHT5WVTrJSU33uT4g=
+github.com/google/go-jsonnet v0.20.0/go.mod h1:VbgWF9JX7ztlv770x/TolZNGGFfiHEVx9G6ca2eUmeA=
 github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
 github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
 github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
@@ -43,6 +45,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 github.com/sblinch/kdl-go v0.0.0-20240410000746-21754ba9ac55 h1:scyq0E9FvdGLX5lxAwjK0HebTM3Y7dG3tYrlXP+x+tk=
 github.com/sblinch/kdl-go v0.0.0-20240410000746-21754ba9ac55/go.mod h1:b3oNGuAKOQzhsCKmuLc/urEOPzgHj6fB8vl8bwTBh28=
+github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
+github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
 github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
@@ -59,13 +63,6 @@ github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
 github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
 github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
 github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
-github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
-go.jolheiser.com/nixfig v0.0.0-20231129190849-8b2170b2ecda/go.mod h1:GlnM735CZXZdJ7v9CMZ8Xjw1gZTrDXJ66Ak+jNwlwNU=
-go.jolheiser.com/nixfig v0.0.0-20240620013146-d3931c448175 h1:PDwpdixizHZQbU6DrXQCtMmnuTCPFivBUw7th2Fs+2k=
-go.jolheiser.com/nixfig v0.0.0-20240620013146-d3931c448175/go.mod h1:ua/+4W7HyAsknnkU2gT2jzuURFx0cImj7Uht27606TY=
-go.jolheiser.com/nixfig v0.0.0-20240620014138-f34fba6b99d6 h1:TB+c0tNI8bLvn+AddMV8Vy2r0zGRmZEkC4yhICrC7ts=
-go.jolheiser.com/nixfig v0.0.0-20240620014138-f34fba6b99d6/go.mod h1:ua/+4W7HyAsknnkU2gT2jzuURFx0cImj7Uht27606TY=
-github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 go.jolheiser.com/nixfig v0.0.0-20240620014425-c1d5ec9d077a/go.mod h1:ua/+4W7HyAsknnkU2gT2jzuURFx0cImj7Uht27606TY=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -77,8 +74,8 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
+golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
-golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
 golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
@@ -91,9 +88,12 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
 gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
+github.com/alecthomas/assert/v2 v2.4.0 h1:/ZiZ0NnriAWPYYO+4eOjgzNELrFQLaHNr92mHSHFj9U=
-github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
 github.com/alecthomas/assert/v2 v2.4.0 h1:/ZiZ0NnriAWPYYO+4eOjgzNELrFQLaHNr92mHSHFj9U=
+gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
+sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=