-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcoder.go
More file actions
73 lines (65 loc) · 2.21 KB
/
coder.go
File metadata and controls
73 lines (65 loc) · 2.21 KB
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
// Package coder provides K8s-style Manifest encoding/decoding for
// Gateway API resources (HTTPRoute, Gateway) used by the Drycc CLI.
//
// A Coder converts between the flat API types used by controller-sdk-go
// and the K8s Manifest format (kind/Metadata/spec/status) used in CLI YAML files.
package coder
import (
"bytes"
"encoding/json"
"fmt"
"strings"
drycc "github.com/drycc/controller-sdk-go"
"gopkg.in/yaml.v3"
)
var APIVersion string = fmt.Sprintf("controller.drycc.cc/v%s", strings.Split(drycc.APIVersion, ".")[0])
// marshalYAML encodes a value to YAML with 2-space indentation and K8s-style
// array formatting (array items are not indented relative to their parent key).
// A JSON round-trip normalizes the entire structure to pure map[string]any /
// []any so that yaml.v3 renders all arrays with consistent compact style.
func marshalYAML(v interface{}) ([]byte, error) {
// Normalize the entire structure to pure map[string]any / []any
data, err := json.Marshal(v)
if err != nil {
return nil, err
}
var normalized any
if err := json.Unmarshal(data, &normalized); err != nil {
return nil, err
}
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
if err := enc.Encode(normalized); err != nil {
return nil, err
}
if err := enc.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// normalize converts a typed struct or slice into plain map[string]any /
// []any via a JSON round-trip so that yaml.v3 renders arrays with consistent
// compact style (no extra indentation for array items).
func normalize(v any) any {
data, err := json.Marshal(v)
if err != nil {
return v
}
var result any
if err := json.Unmarshal(data, &result); err != nil {
return v
}
return result
}
// Manifest is the K8s-style wrapper used for YAML input/output.
type Manifest struct {
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
Kind string `json:"kind" yaml:"kind"`
Metadata Metadata `json:"metadata" yaml:"metadata"`
Spec map[string]any `json:"spec,omitempty" yaml:"spec,omitempty"`
Status map[string]any `json:"status,omitempty" yaml:"status,omitempty"`
}
type Metadata struct {
Name string `json:"name" yaml:"name"`
}