-
Notifications
You must be signed in to change notification settings - Fork 136
/
toml.go
103 lines (82 loc) · 2.7 KB
/
toml.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2021 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package toml
import (
"bytes"
"fmt"
"strings"
"carvel.dev/ytt/pkg/orderedmap"
"carvel.dev/ytt/pkg/template/core"
"carvel.dev/ytt/pkg/yamlmeta"
"carvel.dev/ytt/pkg/yttlibrary"
"github.com/BurntSushi/toml"
"github.com/k14s/starlark-go/starlark"
"github.com/k14s/starlark-go/starlarkstruct"
)
var (
tomlMod = &starlarkstruct.Module{
Name: "toml",
Members: starlark.StringDict{
"encode": starlark.NewBuiltin("toml.encode", core.ErrWrapper(tomlModule{}.Encode)),
"decode": starlark.NewBuiltin("toml.decode", core.ErrWrapper(tomlModule{}.Decode)),
},
}
// TOMLAPI contains the definition of the @ytt:toml module
TOMLAPI = starlark.StringDict{"toml": tomlMod}
)
func init() {
yttlibrary.RegisterExt(tomlMod)
}
type tomlModule struct{}
// Encode is a core.StarlarkFunc that renders the provided input into a TOML formatted string
func (b tomlModule) Encode(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return starlark.None, fmt.Errorf("expected exactly one argument")
}
allowedKWArgs := map[string]struct{}{
"indent": {},
}
if err := core.CheckArgNames(kwargs, allowedKWArgs); err != nil {
return starlark.None, err
}
val, err := core.NewStarlarkValue(args.Index(0)).AsGoValue()
if err != nil {
return starlark.None, err
}
val = orderedmap.Conversion{yamlmeta.NewGoFromAST(val)}.AsUnorderedStringMaps()
indent, err := core.Int64Arg(kwargs, "indent")
if err != nil {
return starlark.None, err
}
if indent < 0 || indent > 8 {
// mitigate https://cwe.mitre.org/data/definitions/409.html
return starlark.None, fmt.Errorf("indent value must be between 0 and 8")
}
var buffer bytes.Buffer
encoder := toml.NewEncoder(&buffer)
if indent > 0 {
encoder.Indent = strings.Repeat(" ", int(indent))
}
err = encoder.Encode(val)
if err != nil {
return starlark.None, err
}
return starlark.String(buffer.String()), nil
}
// Decode is a core.StarlarkFunc that parses the provided input from TOML format into dicts, lists, and scalars
func (b tomlModule) Decode(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return starlark.None, fmt.Errorf("expected exactly one argument")
}
valEncoded, err := core.NewStarlarkValue(args.Index(0)).AsString()
if err != nil {
return starlark.None, err
}
var valDecoded interface{}
err = toml.Unmarshal([]byte(valEncoded), &valDecoded)
if err != nil {
return starlark.None, err
}
valDecoded = orderedmap.Conversion{valDecoded}.FromUnorderedMaps()
return core.NewGoValue(valDecoded).AsStarlarkValue(), nil
}