-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson.go
98 lines (80 loc) · 2.69 KB
/
json.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
package pipeline
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/oleiade/reflections"
)
// inlineFriendlyMarshalJSON marshals the given object to JSON, but with special handling given to fields tagged with ",inline".
// This is needed because yaml.v3 has "inline" but encoding/json has no concept of it.
func inlineFriendlyMarshalJSON(q any) ([]byte, error) {
fieldNames, err := reflections.Fields(q)
if err != nil {
return nil, fmt.Errorf("could not get fields of %T: %w", q, err)
}
var inlineFields map[string]any // no need to pre-allocate, we directly set it if we find inline fields
outlineFields := make(map[string]any, len(fieldNames))
for _, fieldName := range fieldNames {
tag, err := reflections.GetFieldTag(q, fieldName, "yaml")
if err != nil {
return nil, fmt.Errorf("could not get yaml tag of %T.%s: %w", q, fieldName, err)
}
switch tag {
case "-":
continue
case ",inline":
inlineFieldsValue, err := reflections.GetField(q, fieldName)
if err != nil {
return nil, fmt.Errorf("could not get inline fields value of %T.%s: %w", q, fieldName, err)
}
if inf, ok := inlineFieldsValue.(map[string]any); ok {
inlineFields = inf
} else {
return nil, fmt.Errorf("inline fields value of %T.%s must be a map[string]any, was %T instead", q, fieldName, inlineFieldsValue)
}
default:
fieldValue, err := reflections.GetField(q, fieldName)
if err != nil {
return nil, fmt.Errorf("could not get value of %T.%s: %w", q, fieldName, err)
}
tags := strings.Split(tag, ",")
keyName := tags[0] // e.g. "foo,omitempty" -> "foo"
if len(tags) > 1 && tags[1] == "omitempty" && isEmptyValue(fieldValue) {
continue
}
outlineFields[keyName] = fieldValue
}
}
allFields := make(map[string]any, len(outlineFields)+len(inlineFields))
for k, v := range inlineFields {
allFields[k] = v
}
// "outline" (non-inline) fields should take precedence over inline fields
for k, v := range outlineFields {
allFields[k] = v
}
return json.Marshal(allFields)
}
// stolen from encoding/json
func isEmptyValue(q any) bool {
if q == nil { // not stolen from encoding/json, but oddly missing from it?
return true
}
v := reflect.ValueOf(q)
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Pointer:
return v.IsNil()
}
return false
}