2019-10-18 15:20:09 +09:00
|
|
|
package yaml
|
|
|
|
|
|
2020-01-09 13:33:10 +09:00
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
|
|
|
|
|
"github.com/goccy/go-yaml/ast"
|
|
|
|
|
)
|
2019-10-18 15:20:09 +09:00
|
|
|
|
2019-10-21 01:29:32 +09:00
|
|
|
// DecodeOption functional option type for Decoder
|
2019-10-18 15:20:09 +09:00
|
|
|
type DecodeOption func(d *Decoder) error
|
|
|
|
|
|
2019-10-21 01:29:32 +09:00
|
|
|
// ReferenceReaders pass to Decoder that reference to anchor defined by passed readers
|
2019-10-18 15:20:09 +09:00
|
|
|
func ReferenceReaders(readers ...io.Reader) DecodeOption {
|
|
|
|
|
return func(d *Decoder) error {
|
|
|
|
|
d.referenceReaders = append(d.referenceReaders, readers...)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 01:29:32 +09:00
|
|
|
// ReferenceFiles pass to Decoder that reference to anchor defined by passed files
|
2019-10-18 15:20:09 +09:00
|
|
|
func ReferenceFiles(files ...string) DecodeOption {
|
|
|
|
|
return func(d *Decoder) error {
|
|
|
|
|
d.referenceFiles = files
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 01:29:32 +09:00
|
|
|
// ReferenceDirs pass to Decoder that reference to anchor defined by files under the passed dirs
|
2019-10-18 15:20:09 +09:00
|
|
|
func ReferenceDirs(dirs ...string) DecodeOption {
|
|
|
|
|
return func(d *Decoder) error {
|
|
|
|
|
d.referenceDirs = dirs
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 01:29:32 +09:00
|
|
|
// RecursiveDir search yaml file recursively from passed dirs by ReferenceDirs option
|
2019-10-18 15:20:09 +09:00
|
|
|
func RecursiveDir(isRecursive bool) DecodeOption {
|
|
|
|
|
return func(d *Decoder) error {
|
|
|
|
|
d.isRecursiveDir = isRecursive
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-19 18:28:36 +09:00
|
|
|
|
2019-10-26 10:08:44 +09:00
|
|
|
// Validator set StructValidator instance to Decoder
|
|
|
|
|
func Validator(v StructValidator) DecodeOption {
|
|
|
|
|
return func(d *Decoder) error {
|
|
|
|
|
d.validator = v
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-10 18:41:23 +09:00
|
|
|
// DisallowUnknownField causes the Decoder to return an error when the destination
|
|
|
|
|
// is a struct and the input contains object keys which do not match any
|
|
|
|
|
// non-ignored, exported fields in the destination.
|
|
|
|
|
func DisallowUnknownField() DecodeOption {
|
|
|
|
|
return func(d *Decoder) error {
|
|
|
|
|
d.disallowUnknownField = true
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 19:47:47 +09:00
|
|
|
// UseOrderedMap can be interpreted as a map,
|
|
|
|
|
// and uses MapSlice ( ordered map ) aggressively if there is no type specification
|
|
|
|
|
func UseOrderedMap() DecodeOption {
|
|
|
|
|
return func(d *Decoder) error {
|
|
|
|
|
d.useOrderedMap = true
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 01:29:32 +09:00
|
|
|
// EncodeOption functional option type for Encoder
|
2019-10-19 18:28:36 +09:00
|
|
|
type EncodeOption func(e *Encoder) error
|
|
|
|
|
|
2019-10-21 01:29:32 +09:00
|
|
|
// Indent change indent number
|
2019-10-19 18:28:36 +09:00
|
|
|
func Indent(spaces int) EncodeOption {
|
|
|
|
|
return func(e *Encoder) error {
|
|
|
|
|
e.indent = spaces
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-31 13:29:43 +09:00
|
|
|
|
|
|
|
|
// Flow encoding by flow style
|
|
|
|
|
func Flow(isFlowStyle bool) EncodeOption {
|
|
|
|
|
return func(e *Encoder) error {
|
|
|
|
|
e.isFlowStyle = isFlowStyle
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-09 13:33:10 +09:00
|
|
|
|
|
|
|
|
// MarshalAnchor call back if encoder find an anchor during encoding
|
|
|
|
|
func MarshalAnchor(callback func(*ast.AnchorNode, interface{}) error) EncodeOption {
|
|
|
|
|
return func(e *Encoder) error {
|
|
|
|
|
e.anchorCallback = callback
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|