Skip to content

Commit f6b8681

Browse files
committed
Recurse encoding!
1 parent f918f86 commit f6b8681

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

cli/deployment/yaml.go

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ import (
1010
"gopkg.in/yaml.v3"
1111

1212
"github.com/hashicorp/go-multierror"
13-
14-
"github.com/coder/coder/codersdk"
1513
)
1614

17-
// func goNameToYAML(name string) string {
18-
// }
19-
2015
func scalarNode(v any) (*yaml.Node, error) {
2116
var valueStr string
2217
switch v := v.(type) {
@@ -67,15 +62,21 @@ func yamlDeploymentField(field reflect.Value) (*yaml.Node, error) {
6762
}
6863
}
6964

70-
// MarshalYAML converts the deployment config to its MarshalYAML representation.
71-
func MarshalYAML(config *codersdk.DeploymentConfig) ([]byte, error) {
65+
// MarshalYAML converts the deployment config to it's yaml representation.
66+
// It accepts `any` because it calls itself recursively on its values.
67+
func MarshalYAML(config any) (*yaml.Node, error) {
7268
var (
73-
document = yaml.Node{
69+
document = &yaml.Node{
7470
Kind: yaml.MappingNode,
7571
}
76-
configValue = reflect.ValueOf(config).Elem()
72+
configValue = reflect.ValueOf(config)
7773
merr *multierror.Error
7874
)
75+
76+
if configValue.Kind() == reflect.Ptr {
77+
configValue = configValue.Elem()
78+
}
79+
7980
for i := 0; i < configValue.NumField(); i++ {
8081
var (
8182
configField = configValue.Field(i).Elem()
@@ -85,28 +86,38 @@ func MarshalYAML(config *codersdk.DeploymentConfig) ([]byte, error) {
8586

8687
switch {
8788
case strings.HasPrefix(typeName, "codersdk.DeploymentConfigField["):
89+
node, err := yamlDeploymentField(configField)
90+
if err != nil {
91+
merr = multierror.Append(merr, err)
92+
continue
93+
}
8894
// Write field name.
8995
document.Content = append(document.Content, &yaml.Node{
9096
Kind: yaml.ScalarNode,
9197
Value: fieldName,
9298
HeadComment: configField.FieldByName("Usage").String(),
9399
})
94-
node, err := yamlDeploymentField(configField)
100+
101+
// Write node contents.
102+
document.Content = append(document.Content, node)
103+
case configField.Kind() == reflect.Struct:
104+
// Recursively resolve configuration group values.
105+
node, err := MarshalYAML(configField.Interface())
95106
if err != nil {
96-
merr = multierror.Append(merr, err)
107+
merr = multierror.Append(
108+
merr,
109+
xerrors.Errorf("marshal group %s: %w", fieldName, err),
110+
)
97111
continue
98112
}
99-
// Write node contents.
113+
document.Content = append(document.Content, &yaml.Node{
114+
Kind: yaml.ScalarNode,
115+
Value: fieldName,
116+
})
100117
document.Content = append(document.Content, node)
101118
default:
102119
merr = multierror.Append(merr, xerrors.Errorf("unsupported type: %s", typeName))
103120
}
104121
}
105-
byt, err := yaml.Marshal(document)
106-
if err != nil {
107-
merr = multierror.Append(
108-
merr, xerrors.Errorf("marshal failed: %w\n%+v", err, document),
109-
)
110-
}
111-
return byt, merr.ErrorOrNil()
122+
return document, merr.ErrorOrNil()
112123
}

cli/deployment/yaml_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ func TestMarshalYAML(t *testing.T) {
1717
config := deployment.DefaultConfig()
1818
// For testing array marshaling.
1919
config.ProxyTrustedHeaders.Value = []string{"X-Forwarded-For", "X-Forwarded-Proto"}
20-
byt, err := deployment.MarshalYAML(config)
20+
y, err := deployment.MarshalYAML(config)
21+
require.NoError(t, err)
22+
23+
byt, err := yaml.Marshal(y)
24+
require.NoError(t, err)
25+
2126
t.Logf("yaml:\n%s", string(byt))
2227
require.NoError(t, err)
2328
})

0 commit comments

Comments
 (0)