Skip to content

Commit 26e7e82

Browse files
authored
[cuecfg] add toml support (#92)
## Summary We need this to parse Cargo.toml files. I chose this library: [github.com/pelletier/go-toml](https://github.com/pelletier/go-toml). - The alternative is https://github.com/BurntSushi/toml. This one has more github stars than `go-toml`. - `go-toml`'s interface is more similar to `encoding/json`, and is also used by `viper`. Also, changed `v` to `value` in `cuecfg.go` to address linter complaint. ## How was it tested? `go test ./cuecfg/...`
1 parent f74eb42 commit 26e7e82

File tree

3 files changed

+87
-14
lines changed

3 files changed

+87
-14
lines changed

cuecfg/cuecfg.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,41 @@ import (
1111
"github.com/pkg/errors"
1212
)
1313

14-
// TODO: add support for .cue and possible .toml
14+
// TODO: add support for .cue
1515

16-
func Marshal(v any, extension string) ([]byte, error) {
17-
err := cuego.Complete(v)
16+
func Marshal(value any, extension string) ([]byte, error) {
17+
err := cuego.Complete(value)
1818
if err != nil {
1919
return nil, errors.WithStack(err)
2020
}
2121

2222
switch extension {
2323
case ".json":
24-
return MarshalJSON(v)
24+
return MarshalJSON(value)
2525
case ".yml", ".yaml":
26-
return MarshalYaml(v)
26+
return MarshalYaml(value)
27+
case ".toml":
28+
return MarshalToml(value)
2729
}
2830
return nil, errors.Errorf("Unsupported file format '%s' for config file", extension)
2931
}
3032

31-
func Unmarshal(data []byte, extension string, v any) error {
33+
func Unmarshal(data []byte, extension string, value any) error {
3234
switch extension {
3335
case ".json":
34-
err := UnmarshalJSON(data, v)
36+
err := UnmarshalJSON(data, value)
3537
if err != nil {
3638
return errors.WithStack(err)
3739
}
3840
return nil
3941
case ".yml", ".yaml":
40-
err := UnmarshalYaml(data, v)
42+
err := UnmarshalYaml(data, value)
43+
if err != nil {
44+
return errors.WithStack(err)
45+
}
46+
return nil
47+
case ".toml":
48+
err := UnmarshalToml(data, value)
4149
if err != nil {
4250
return errors.WithStack(err)
4351
}
@@ -46,32 +54,32 @@ func Unmarshal(data []byte, extension string, v any) error {
4654
return errors.Errorf("Unsupported file format '%s' for config file", extension)
4755
}
4856

49-
func InitFile(path string, v any) (bool, error) {
57+
func InitFile(path string, value any) (bool, error) {
5058
if _, err := os.Stat(path); err == nil {
5159
// File already exists, don't create a new one.
5260
// TODO: should we read and write again, in case the schema needs updating?
5361
return false, nil
5462
} else if errors.Is(err, os.ErrNotExist) {
5563
// File does not exist, create a new one:
56-
return true, WriteFile(path, v)
64+
return true, WriteFile(path, value)
5765
} else {
5866
// Error case:
5967
return false, errors.WithStack(err)
6068
}
6169

6270
}
6371

64-
func ReadFile(path string, v any) error {
72+
func ReadFile(path string, value any) error {
6573
data, err := os.ReadFile(path)
6674
if err != nil {
6775
return errors.WithStack(err)
6876
}
6977

70-
return Unmarshal(data, filepath.Ext(path), v)
78+
return Unmarshal(data, filepath.Ext(path), value)
7179
}
7280

73-
func WriteFile(path string, v any) error {
74-
data, err := Marshal(v, filepath.Ext(path))
81+
func WriteFile(path string, value any) error {
82+
data, err := Marshal(value, filepath.Ext(path))
7583
if err != nil {
7684
return errors.WithStack(err)
7785
}

cuecfg/cuecfg_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cuecfg
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
type Metadata struct {
10+
Tags []string
11+
}
12+
13+
type MyConfig struct {
14+
Version int
15+
Name string
16+
Meta *Metadata
17+
}
18+
19+
var testTomlCfg = &MyConfig{
20+
Version: 2,
21+
Name: "go-toml",
22+
Meta: &Metadata{
23+
Tags: []string{"go", "toml"},
24+
},
25+
}
26+
27+
var testTomlStr = `Version = 2
28+
Name = 'go-toml'
29+
30+
[Meta]
31+
Tags = ['go', 'toml']
32+
`
33+
34+
func TestMarshalToml(t *testing.T) {
35+
req := require.New(t)
36+
37+
bytes, err := Marshal(testTomlCfg, ".toml")
38+
req.NoError(err)
39+
40+
req.Equal(testTomlStr, string(bytes))
41+
}
42+
43+
func TestUnmarshalToml(t *testing.T) {
44+
req := require.New(t)
45+
cfg := &MyConfig{}
46+
err := Unmarshal([]byte(testTomlStr), ".toml", cfg)
47+
req.NoError(err)
48+
req.Equal(testTomlCfg, cfg)
49+
}

cuecfg/toml.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved.
2+
// Use of this source code is governed by the license in the LICENSE file.
3+
4+
package cuecfg
5+
6+
import (
7+
"github.com/pelletier/go-toml/v2"
8+
)
9+
10+
func MarshalToml(v interface{}) ([]byte, error) {
11+
return toml.Marshal(v)
12+
}
13+
14+
func UnmarshalToml(data []byte, v interface{}) error {
15+
return toml.Unmarshal(data, v)
16+
}

0 commit comments

Comments
 (0)