| 1 | package yaml |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "reflect" |
| 7 | |
| 8 | "github.com/goccy/go-yaml/ast" |
| 9 | ) |
| 10 | |
| 11 | // DecodeOption functional option type for Decoder |
| 12 | type DecodeOption func(d *Decoder) error |
| 13 | |
| 14 | // ReferenceReaders pass to Decoder that reference to anchor defined by passed readers |
| 15 | func ReferenceReaders(readers ...io.Reader) DecodeOption { |
| 16 | return func(d *Decoder) error { |
| 17 | d.referenceReaders = append(d.referenceReaders, readers...) |
| 18 | return nil |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // ReferenceFiles pass to Decoder that reference to anchor defined by passed files |
| 23 | func ReferenceFiles(files ...string) DecodeOption { |
| 24 | return func(d *Decoder) error { |
| 25 | d.referenceFiles = files |
| 26 | return nil |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // ReferenceDirs pass to Decoder that reference to anchor defined by files under the passed dirs |
| 31 | func ReferenceDirs(dirs ...string) DecodeOption { |
| 32 | return func(d *Decoder) error { |
| 33 | d.referenceDirs = dirs |
| 34 | return nil |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // RecursiveDir search yaml file recursively from passed dirs by ReferenceDirs option |
| 39 | func RecursiveDir(isRecursive bool) DecodeOption { |
| 40 | return func(d *Decoder) error { |
| 41 | d.isRecursiveDir = isRecursive |
| 42 | return nil |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Validator set StructValidator instance to Decoder |
| 47 | func Validator(v StructValidator) DecodeOption { |
| 48 | return func(d *Decoder) error { |
| 49 | d.validator = v |
| 50 | return nil |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Strict enable DisallowUnknownField |
| 55 | func Strict() DecodeOption { |
| 56 | return func(d *Decoder) error { |
| 57 | d.disallowUnknownField = 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 | // AllowDuplicateMapKey ignore syntax error when mapping keys that are duplicates. |
| 73 | func AllowDuplicateMapKey() DecodeOption { |
| 74 | return func(d *Decoder) error { |
| 75 | d.allowDuplicateMapKey = 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(ctx context.Context, v interface{}, b []byte) error { |
| 106 | return unmarshaler(v.(*T), b) |
| 107 | } |
| 108 | return nil |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // CustomUnmarshalerContext overrides any decoding process for the type specified in generics. |
| 113 | // Similar to CustomUnmarshaler, but allows passing a context to the unmarshaler function. |
| 114 | func CustomUnmarshalerContext[T any](unmarshaler func(context.Context, *T, []byte) error) DecodeOption { |
| 115 | return func(d *Decoder) error { |
| 116 | var typ *T |
| 117 | d.customUnmarshalerMap[reflect.TypeOf(typ)] = func(ctx context.Context, v interface{}, b []byte) error { |
| 118 | return unmarshaler(ctx, v.(*T), b) |
| 119 | } |
| 120 | return nil |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // EncodeOption functional option type for Encoder |
| 125 | type EncodeOption func(e *Encoder) error |
| 126 | |
| 127 | // Indent change indent number |
| 128 | func Indent(spaces int) EncodeOption { |
| 129 | return func(e *Encoder) error { |
| 130 | e.indentNum = spaces |
| 131 | return nil |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // IndentSequence causes sequence values to be indented the same value as Indent |
| 136 | func IndentSequence(indent bool) EncodeOption { |
| 137 | return func(e *Encoder) error { |
| 138 | e.indentSequence = indent |
| 139 | return nil |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // UseSingleQuote determines if single or double quotes should be preferred for strings. |
| 144 | func UseSingleQuote(sq bool) EncodeOption { |
| 145 | return func(e *Encoder) error { |
| 146 | e.singleQuote = sq |
| 147 | return nil |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Flow encoding by flow style |
| 152 | func Flow(isFlowStyle bool) EncodeOption { |
| 153 | return func(e *Encoder) error { |
| 154 | e.isFlowStyle = isFlowStyle |
| 155 | return nil |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // WithSmartAnchor when multiple map values share the same pointer, |
| 160 | // an anchor is automatically assigned to the first occurrence, and aliases are used for subsequent elements. |
| 161 | // The map key name is used as the anchor name by default. |
| 162 | // If key names conflict, a suffix is automatically added to avoid collisions. |
| 163 | // This is an experimental feature and cannot be used simultaneously with anchor tags. |
| 164 | func WithSmartAnchor() EncodeOption { |
| 165 | return func(e *Encoder) error { |
| 166 | e.enableSmartAnchor = true |
| 167 | return nil |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // UseLiteralStyleIfMultiline causes encoding multiline strings with a literal syntax, |
| 172 | // no matter what characters they include |
| 173 | func UseLiteralStyleIfMultiline(useLiteralStyleIfMultiline bool) EncodeOption { |
| 174 | return func(e *Encoder) error { |
| 175 | e.useLiteralStyleIfMultiline = useLiteralStyleIfMultiline |
| 176 | return nil |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // JSON encode in JSON format |
| 181 | func JSON() EncodeOption { |
| 182 | return func(e *Encoder) error { |
| 183 | e.isJSONStyle = true |
| 184 | e.isFlowStyle = true |
| 185 | return nil |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // MarshalAnchor call back if encoder find an anchor during encoding |
| 190 | func MarshalAnchor(callback func(*ast.AnchorNode, interface{}) error) EncodeOption { |
| 191 | return func(e *Encoder) error { |
| 192 | e.anchorCallback = callback |
| 193 | return nil |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // UseJSONMarshaler if neither `BytesMarshaler` nor `InterfaceMarshaler` |
| 198 | // nor `encoding.TextMarshaler` is implemented and `MarshalJSON()([]byte, error)` is implemented, |
| 199 | // call `MarshalJSON` to convert the returned `JSON` to `YAML` for processing. |
| 200 | func UseJSONMarshaler() EncodeOption { |
| 201 | return func(e *Encoder) error { |
| 202 | e.useJSONMarshaler = true |
| 203 | return nil |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // CustomMarshaler overrides any encoding process for the type specified in generics. |
| 208 | // |
| 209 | // NOTE: If type T implements MarshalYAML for pointer receiver, the type specified in CustomMarshaler must be *T. |
| 210 | // If RegisterCustomMarshaler and CustomMarshaler of EncodeOption are specified for the same type, |
| 211 | // the CustomMarshaler specified in EncodeOption takes precedence. |
| 212 | func CustomMarshaler[T any](marshaler func(T) ([]byte, error)) EncodeOption { |
| 213 | return func(e *Encoder) error { |
| 214 | var typ T |
| 215 | e.customMarshalerMap[reflect.TypeOf(typ)] = func(ctx context.Context, v interface{}) ([]byte, error) { |
| 216 | return marshaler(v.(T)) |
| 217 | } |
| 218 | return nil |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // CustomMarshalerContext overrides any encoding process for the type specified in generics. |
| 223 | // Similar to CustomMarshaler, but allows passing a context to the marshaler function. |
| 224 | func CustomMarshalerContext[T any](marshaler func(context.Context, T) ([]byte, error)) EncodeOption { |
| 225 | return func(e *Encoder) error { |
| 226 | var typ T |
| 227 | e.customMarshalerMap[reflect.TypeOf(typ)] = func(ctx context.Context, v interface{}) ([]byte, error) { |
| 228 | return marshaler(ctx, v.(T)) |
| 229 | } |
| 230 | return nil |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // AutoInt automatically converts floating-point numbers to integers when the fractional part is zero. |
| 235 | // For example, a value of 1.0 will be encoded as 1. |
| 236 | func AutoInt() EncodeOption { |
| 237 | return func(e *Encoder) error { |
| 238 | e.autoInt = true |
| 239 | return nil |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // OmitEmpty behaves in the same way as the interpretation of the omitempty tag in the encoding/json library. |
| 244 | // set on all the fields. |
| 245 | // In the current implementation, the omitempty tag is not implemented in the same way as encoding/json, |
| 246 | // so please specify this option if you expect the same behavior. |
| 247 | func OmitEmpty() EncodeOption { |
| 248 | return func(e *Encoder) error { |
| 249 | e.omitEmpty = true |
| 250 | return nil |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // OmitZero forces the encoder to assume an `omitzero` struct tag is |
| 255 | // set on all the fields. See `Marshal` commentary for the `omitzero` tag logic. |
| 256 | func OmitZero() EncodeOption { |
| 257 | return func(e *Encoder) error { |
| 258 | e.omitZero = true |
| 259 | return nil |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // CommentPosition type of the position for comment. |
| 264 | type CommentPosition int |
| 265 | |
| 266 | const ( |
| 267 | CommentHeadPosition CommentPosition = CommentPosition(iota) |
| 268 | CommentLinePosition |
| 269 | CommentFootPosition |
| 270 | ) |
| 271 | |
| 272 | func (p CommentPosition) String() string { |
| 273 | switch p { |
| 274 | case CommentHeadPosition: |
| 275 | return "Head" |
| 276 | case CommentLinePosition: |
| 277 | return "Line" |
| 278 | case CommentFootPosition: |
| 279 | return "Foot" |
| 280 | default: |
| 281 | return "" |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // LineComment create a one-line comment for CommentMap. |
| 286 | func LineComment(text string) *Comment { |
| 287 | return &Comment{ |
| 288 | Texts: []string{text}, |
| 289 | Position: CommentLinePosition, |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // HeadComment create a multiline comment for CommentMap. |
| 294 | func HeadComment(texts ...string) *Comment { |
| 295 | return &Comment{ |
| 296 | Texts: texts, |
| 297 | Position: CommentHeadPosition, |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // FootComment create a multiline comment for CommentMap. |
| 302 | func FootComment(texts ...string) *Comment { |
| 303 | return &Comment{ |
| 304 | Texts: texts, |
| 305 | Position: CommentFootPosition, |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // Comment raw data for comment. |
| 310 | type Comment struct { |
| 311 | Texts []string |
| 312 | Position CommentPosition |
| 313 | } |
| 314 | |
| 315 | // CommentMap map of the position of the comment and the comment information. |
| 316 | type CommentMap map[string][]*Comment |
| 317 | |
| 318 | // WithComment add a comment using the location and text information given in the CommentMap. |
| 319 | func WithComment(cm CommentMap) EncodeOption { |
| 320 | return func(e *Encoder) error { |
| 321 | commentMap := map[*Path][]*Comment{} |
| 322 | for k, v := range cm { |
| 323 | path, err := PathString(k) |
| 324 | if err != nil { |
| 325 | return err |
| 326 | } |
| 327 | commentMap[path] = v |
| 328 | } |
| 329 | e.commentMap = commentMap |
| 330 | return nil |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // CommentToMap apply the position and content of comments in a YAML document to a CommentMap. |
| 335 | func CommentToMap(cm CommentMap) DecodeOption { |
| 336 | return func(d *Decoder) error { |
| 337 | if cm == nil { |
| 338 | return ErrInvalidCommentMapValue |
| 339 | } |
| 340 | d.toCommentMap = cm |
| 341 | return nil |
| 342 | } |
| 343 | } |