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

View File

@ -24,7 +24,7 @@ func maine() error {
unmarshal = e.Unmarshal
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)
toFunc := func(s string) error {
e, err := cfg.ParseEncoding(s)
@ -34,7 +34,7 @@ func maine() error {
marshal = e.Marshal
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)
if err := fs.Parse(os.Args[1:]); err != nil {
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/pelletier/go-toml/v2 v2.2.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
go.jolheiser.com/nixfig v0.0.0-20231129190849-8b2170b2ecda
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/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/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=