feat: kdl

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 2024-06-07 19:05:59 -05:00
parent ae439db5e5
commit c3e43f01e0
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
6 changed files with 18 additions and 3 deletions

8
cfg.go
View File

@ -7,6 +7,7 @@ import (
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
"github.com/philandstuff/dhall-golang/v6" "github.com/philandstuff/dhall-golang/v6"
"github.com/sblinch/kdl-go"
"github.com/tailscale/hujson" "github.com/tailscale/hujson"
"go.jolheiser.com/nixfig" "go.jolheiser.com/nixfig"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@ -22,6 +23,7 @@ const (
TOML Encoding = "toml" TOML Encoding = "toml"
NIX Encoding = "nix" NIX Encoding = "nix"
DHALL Encoding = "dhall" DHALL Encoding = "dhall"
KDL Encoding = "kdl"
) )
// Marshal takes data and encodes for an [Encoding] // Marshal takes data and encodes for an [Encoding]
@ -49,6 +51,8 @@ func ParseEncoding(s string) (Encoding, error) {
return NIX, nil return NIX, nil
case "dhall": case "dhall":
return DHALL, nil return DHALL, nil
case "kdl":
return KDL, nil
default: default:
return "", fmt.Errorf("unknown encoding %q", s) return "", fmt.Errorf("unknown encoding %q", s)
} }
@ -65,6 +69,8 @@ func Marshal(e Encoding, v any) ([]byte, error) {
return toml.Marshal(v) return toml.Marshal(v)
case NIX: case NIX:
return nixfig.Marshal(v) return nixfig.Marshal(v)
case KDL:
return kdl.Marshal(v)
default: default:
return nil, fmt.Errorf("unknown marshal format %q", e) return nil, fmt.Errorf("unknown marshal format %q", e)
} }
@ -87,6 +93,8 @@ func Unmarshal(e Encoding, data []byte, v any) error {
return nixfig.Unmarshal(data, v) return nixfig.Unmarshal(data, v)
case DHALL: case DHALL:
return dhall.Unmarshal(data, v) return dhall.Unmarshal(data, v)
case KDL:
return kdl.Unmarshal(data, v)
default: default:
return fmt.Errorf("unknown unmarshal format %q", e) return fmt.Errorf("unknown unmarshal format %q", e)

View File

@ -47,7 +47,7 @@ func TestEncoding(t *testing.T) {
}, },
} }
encoders := []Encoding{JSON, JSONC, YAML, TOML} encoders := []Encoding{JSON, JSONC, YAML, TOML, KDL}
// Only test nix if it's available // Only test nix if it's available
if nixfig.Nix != "" { if nixfig.Nix != "" {
encoders = append(encoders, NIX) encoders = append(encoders, NIX)

View File

@ -24,7 +24,7 @@ func maine() error {
unmarshal = e.Unmarshal unmarshal = e.Unmarshal
return nil return nil
} }
fs.Func("from", "The format to convert from [json(c), yaml, toml, nix, dhall]", fromFunc) fs.Func("from", "The format to convert from [json(c), yaml, toml, nix, dhall, kdl]", fromFunc)
fs.Func("f", "--from", fromFunc) fs.Func("f", "--from", fromFunc)
toFunc := func(s string) error { toFunc := func(s string) error {
e, err := cfg.ParseEncoding(s) e, err := cfg.ParseEncoding(s)
@ -34,7 +34,7 @@ func maine() error {
marshal = e.Marshal marshal = e.Marshal
return nil return nil
} }
fs.Func("to", "The format to convert to [json, yaml, toml, nix]", toFunc) fs.Func("to", "The format to convert to [json, yaml, toml, nix, kdl]", toFunc)
fs.Func("t", "--to", toFunc) fs.Func("t", "--to", toFunc)
if err := fs.Parse(os.Args[1:]); err != nil { if err := fs.Parse(os.Args[1:]); err != nil {
return err return err

View File

@ -0,0 +1,4 @@
admins "jolheiser" "admin"
email "jolheiser@example.com"
username "jolheiser"
home "/home/jolheiser"

1
go.mod
View File

@ -6,6 +6,7 @@ require (
github.com/matryer/is v1.4.1 github.com/matryer/is v1.4.1
github.com/pelletier/go-toml/v2 v2.2.2 github.com/pelletier/go-toml/v2 v2.2.2
github.com/philandstuff/dhall-golang/v6 v6.0.2 github.com/philandstuff/dhall-golang/v6 v6.0.2
github.com/sblinch/kdl-go v0.0.0-20240410000746-21754ba9ac55
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a
go.jolheiser.com/nixfig v0.0.0-20231129190849-8b2170b2ecda go.jolheiser.com/nixfig v0.0.0-20231129190849-8b2170b2ecda
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1

2
go.sum
View File

@ -41,6 +41,8 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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/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/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= 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.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=