| 1 |
package yaml |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"reflect" |
| 6 |
|
| 7 |
"github.com/goccy/go-yaml/ast" |
| 8 |
) |
| 9 |
|
| 10 |
// DecodeOption functional option type for Decoder |
| 11 |
type DecodeOption func(d *Decoder) error |
| 12 |
|
| 13 |
// ReferenceReaders pass to Decoder that reference to anchor defined by passed readers |
| 14 |
func ReferenceReaders(readers ...io.Reader) DecodeOption { |
| 15 |
return func(d *Decoder) error { |
| 16 |
d.referenceReaders = append(d.referenceReaders, readers...) |
| 17 |
return nil |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
// ReferenceFiles pass to Decoder that reference to anchor defined by passed files |
| 22 |
func ReferenceFiles(files ...string) DecodeOption { |
| 23 |
return func(d *Decoder) error { |
| 24 |
d.referenceFiles = files |
| 25 |
return nil |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
// ReferenceDirs pass to Decoder that reference to anchor defined by files under the passed dirs |
| 30 |
func ReferenceDirs(dirs ...string) DecodeOption { |
| 31 |
return func(d *Decoder) error { |
| 32 |
d.referenceDirs = dirs |
| 33 |
return nil |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
// RecursiveDir search yaml file recursively from passed dirs by ReferenceDirs option |
| 38 |
func RecursiveDir(isRecursive bool) DecodeOption { |
| 39 |
return func(d *Decoder) error { |
| 40 |
d.isRecursiveDir = isRecursive |
| 41 |
return nil |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// Validator set StructValidator instance to Decoder |
| 46 |
func Validator(v StructValidator) DecodeOption { |
| 47 |
return func(d *Decoder) error { |
| 48 |
d.validator = v |
| 49 |
return nil |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Strict enable DisallowUnknownField and DisallowDuplicateKey |
| 54 |
func Strict() DecodeOption { |
| 55 |
return func(d *Decoder) error { |
| 56 |
d.disallowUnknownField = true |
| 57 |
d.disallowDuplicateKey = true |
| 58 |
return nil |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// DisallowUnknownField causes the Decoder to return an error when the destination |
| 63 |
// is a struct and the input contains object keys which do not match any |
| 64 |
// non-ignored, exported fields in the destination. |
| 65 |
func DisallowUnknownField() DecodeOption { |
| 66 |
return func(d *Decoder) error { |
| 67 |
d.disallowUnknownField = true |
| 68 |
return nil |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// DisallowDuplicateKey causes an error when mapping keys that are duplicates |
| 73 |
func DisallowDuplicateKey() DecodeOption { |
| 74 |
return func(d *Decoder) error { |
| 75 |
d.disallowDuplicateKey = true |
| 76 |
return nil |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// UseOrderedMap can be interpreted as a map, |
| 81 |
// and uses MapSlice ( ordered map ) aggressively if there is no type specification |
| 82 |
func UseOrderedMap() DecodeOption { |
| 83 |
return func(d *Decoder) error { |
| 84 |
d.useOrderedMap = true |
| 85 |
return nil |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// UseJSONUnmarshaler if neither `BytesUnmarshaler` nor `InterfaceUnmarshaler` is implemented |
| 90 |
// and `UnmashalJSON([]byte)error` is implemented, convert the argument from `YAML` to `JSON` and then call it. |
| 91 |
func UseJSONUnmarshaler() DecodeOption { |
| 92 |
return func(d *Decoder) error { |
| 93 |
d.useJSONUnmarshaler = true |
| 94 |
return nil |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// CustomUnmarshaler overrides any decoding process for the type specified in generics. |
| 99 |
// |
| 100 |
// NOTE: If RegisterCustomUnmarshaler and CustomUnmarshaler of DecodeOption are specified for the same type, |
| 101 |
// the CustomUnmarshaler specified in DecodeOption takes precedence. |
| 102 |
func CustomUnmarshaler[T any](unmarshaler func(*T, []byte) error) DecodeOption { |
| 103 |
return func(d *Decoder) error { |
| 104 |
var typ *T |
| 105 |
d.customUnmarshalerMap[reflect.TypeOf(typ)] = func(v interface{}, b []byte) error { |
| 106 |
return unmarshaler(v.(*T), b) |
| 107 |
} |
| 108 |
return nil |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// EncodeOption functional option type for Encoder |
| 113 |
type EncodeOption func(e *Encoder) error |
| 114 |
|
| 115 |
// Indent change indent number |
| 116 |
func Indent(spaces int) EncodeOption { |
| 117 |
return func(e *Encoder) error { |
| 118 |
e.indent = spaces |
| 119 |
return nil |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// IndentSequence causes sequence values to be indented the same value as Indent |
| 124 |
func IndentSequence(indent bool) EncodeOption { |
| 125 |
return func(e *Encoder) error { |
| 126 |
e.indentSequence = indent |
| 127 |
return nil |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// UseSingleQuote determines if single or double quotes should be preferred for strings. |
| 132 |
func UseSingleQuote(sq bool) EncodeOption { |
| 133 |
return func(e *Encoder) error { |
| 134 |
e.singleQuote = sq |
| 135 |
return nil |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
// Flow encoding by flow style |
| 140 |
func Flow(isFlowStyle bool) EncodeOption { |
| 141 |
return func(e *Encoder) error { |
| 142 |
e.isFlowStyle = isFlowStyle |
| 143 |
return nil |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
// UseLiteralStyleIfMultiline causes encoding multiline strings with a literal syntax, |
| 148 |
// no matter what characters they include |
| 149 |
func UseLiteralStyleIfMultiline(useLiteralStyleIfMultiline bool) EncodeOption { |
| 150 |
return func(e *Encoder) error { |
| 151 |
e.useLiteralStyleIfMultiline = useLiteralStyleIfMultiline |
| 152 |
return nil |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
// JSON encode in JSON format |
| 157 |
func JSON() EncodeOption { |
| 158 |
return func(e *Encoder) error { |
| 159 |
e.isJSONStyle = true |
| 160 |
e.isFlowStyle = true |
| 161 |
return nil |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
// MarshalAnchor call back if encoder find an anchor during encoding |
| 166 |
func MarshalAnchor(callback func(*ast.AnchorNode, interface{}) error) EncodeOption { |
| 167 |
return func(e *Encoder) error { |
| 168 |
e.anchorCallback = callback |
| 169 |
return nil |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
// UseJSONMarshaler if neither `BytesMarshaler` nor `InterfaceMarshaler` |
| 174 |
// nor `encoding.TextMarshaler` is implemented and `MarshalJSON()([]byte, error)` is implemented, |
| 175 |
// call `MarshalJSON` to convert the returned `JSON` to `YAML` for processing. |
| 176 |
func UseJSONMarshaler() EncodeOption { |
| 177 |
return func(e *Encoder) error { |
| 178 |
e.useJSONMarshaler = true |
| 179 |
return nil |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
// CustomMarshaler overrides any encoding process for the type specified in generics. |
| 184 |
// |
| 185 |
// NOTE: If type T implements MarshalYAML for pointer receiver, the type specified in CustomMarshaler must be *T. |
| 186 |
// If RegisterCustomMarshaler and CustomMarshaler of EncodeOption are specified for the same type, |
| 187 |
// the CustomMarshaler specified in EncodeOption takes precedence. |
| 188 |
func CustomMarshaler[T any](marshaler func(T) ([]byte, error)) EncodeOption { |
| 189 |
return func(e *Encoder) error { |
| 190 |
var typ T |
| 191 |
e.customMarshalerMap[reflect.TypeOf(typ)] = func(v interface{}) ([]byte, error) { |
| 192 |
return marshaler(v.(T)) |
| 193 |
} |
| 194 |
return nil |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
// CommentPosition type of the position for comment. |
| 199 |
type CommentPosition int |
| 200 |
|
| 201 |
const ( |
| 202 |
CommentHeadPosition CommentPosition = CommentPosition(iota) |
| 203 |
CommentLinePosition |
| 204 |
CommentFootPosition |
| 205 |
) |
| 206 |
|
| 207 |
func (p CommentPosition) String() string { |
| 208 |
switch p { |
| 209 |
case CommentHeadPosition: |
| 210 |
return "Head" |
| 211 |
case CommentLinePosition: |
| 212 |
return "Line" |
| 213 |
case CommentFootPosition: |
| 214 |
return "Foot" |
| 215 |
default: |
| 216 |
return "" |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
// LineComment create a one-line comment for CommentMap. |
| 221 |
func LineComment(text string) *Comment { |
| 222 |
return &Comment{ |
| 223 |
Texts: []string{text}, |
| 224 |
Position: CommentLinePosition, |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
// HeadComment create a multiline comment for CommentMap. |
| 229 |
func HeadComment(texts ...string) *Comment { |
| 230 |
return &Comment{ |
| 231 |
Texts: texts, |
| 232 |
Position: CommentHeadPosition, |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
// FootComment create a multiline comment for CommentMap. |
| 237 |
func FootComment(texts ...string) *Comment { |
| 238 |
return &Comment{ |
| 239 |
Texts: texts, |
| 240 |
Position: CommentFootPosition, |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
// Comment raw data for comment. |
| 245 |
type Comment struct { |
| 246 |
Texts []string |
| 247 |
Position CommentPosition |
| 248 |
} |
| 249 |
|
| 250 |
// CommentMap map of the position of the comment and the comment information. |
| 251 |
type CommentMap map[string][]*Comment |
| 252 |
|
| 253 |
// WithComment add a comment using the location and text information given in the CommentMap. |
| 254 |
func WithComment(cm CommentMap) EncodeOption { |
| 255 |
return func(e *Encoder) error { |
| 256 |
commentMap := map[*Path][]*Comment{} |
| 257 |
for k, v := range cm { |
| 258 |
path, err := PathString(k) |
| 259 |
if err != nil { |
| 260 |
return err |
| 261 |
} |
| 262 |
commentMap[path] = v |
| 263 |
} |
| 264 |
e.commentMap = commentMap |
| 265 |
return nil |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
// CommentToMap apply the position and content of comments in a YAML document to a CommentMap. |
| 270 |
func CommentToMap(cm CommentMap) DecodeOption { |
| 271 |
return func(d *Decoder) error { |
| 272 |
if cm == nil { |
| 273 |
return ErrInvalidCommentMapValue |
| 274 |
} |
| 275 |
d.toCommentMap = cm |
| 276 |
return nil |
| 277 |
} |
| 278 |
} |