rs

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2021 License: Apache-2.0 Imports: 11 Imported by: 38

README

rs

CI PkgGoDev GoReportCard Coverage

Rendering Suffix Support for yaml

LICENSE

Copyright 2021 The arhat.dev Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Overview

Package rs provides rendering suffix support for yaml

Index

Constants

View Source
const (
	TagNameRS = "rs"
	TagName   = TagNameRS
)

Variables

View Source
var (
	ErrInterfaceTypeNotHandled = errors.New("interface type not handled")
)

Functions

func InitRecursively

func InitRecursively(fv reflect.Value, h InterfaceTypeHandler)

func MergeMap

func MergeMap(
	original, additional map[string]interface{},

	appendList bool,
	uniqueInListItems bool,
) (map[string]interface{}, error)

Types

type AnyObject

type AnyObject struct {
	// contains filtered or unexported fields
}

AnyObject is a `interface{}` equivalent with rendering suffix support

func (*AnyObject) MarshalJSON

func (o *AnyObject) MarshalJSON() ([]byte, error)

func (*AnyObject) MarshalYAML

func (o *AnyObject) MarshalYAML() (interface{}, error)

func (*AnyObject) ResolveFields

func (o *AnyObject) ResolveFields(rc RenderingHandler, depth int, fieldNames ...string) error

func (*AnyObject) UnmarshalYAML

func (o *AnyObject) UnmarshalYAML(n *yaml.Node) error

func (*AnyObject) Value

func (o *AnyObject) Value() interface{}

type BaseField

type BaseField struct {
	// contains filtered or unexported fields
}

func (*BaseField) HasUnresolvedField

func (f *BaseField) HasUnresolvedField() bool

func (*BaseField) Inherit

func (f *BaseField) Inherit(b *BaseField) error

func (*BaseField) ResolveFields

func (f *BaseField) ResolveFields(rc RenderingHandler, depth int, fieldNames ...string) error

func (*BaseField) UnmarshalYAML

func (f *BaseField) UnmarshalYAML(n *yaml.Node) error

UnmarshalYAML handles parsing of rendering suffix and normal yaml unmarshaling nolint:gocyclo

type Field

type Field interface {
	yaml.Unmarshaler

	// ResolveFields resolves yaml fields using rendering suffix
	// when depth >= 1, resolve inner fields until reaching depth limit
	// when depth == 0, do nothing
	// when depth < 0, resolve recursively
	//
	// when fieldName is not empty, resolve single field
	// when fieldName is empty, resolve all fields in the struct
	ResolveFields(rc RenderingHandler, depth int, fieldNames ...string) error
}

func Init

func Init(in Field, h InterfaceTypeHandler) Field

Init the BaseField embedded in your struct, the BaseField must be the first field

type Foo struct {
	field.BaseField // or *field.BaseField
}

if the arg `in` doesn't contain BaseField or the BaseField is not the first element it does nothing and will return `in` as is.

type InterfaceTypeHandleFunc

type InterfaceTypeHandleFunc func(typ reflect.Type, yamlKey string) (interface{}, error)

func (InterfaceTypeHandleFunc) Create

func (f InterfaceTypeHandleFunc) Create(typ reflect.Type, yamlKey string) (interface{}, error)

type InterfaceTypeHandler

type InterfaceTypeHandler interface {
	// Create request interface type using yaml information
	Create(typ reflect.Type, yamlKey string) (interface{}, error)
}

type JSONPatchSpec

type JSONPatchSpec struct {
	BaseField `yaml:"-" json:"-"`

	Operation string `yaml:"op" json:"op"`

	Path string `yaml:"path" json:"path"`

	Value interface{} `yaml:"value,omitempty" json:"value,omitempty"`
}

JSONPatchSpec per rfc6902

type MergeSource

type MergeSource struct {
	BaseField `yaml:"-" json:"-"`

	Data interface{} `yaml:"data,omitempty"`
}

type PatchSpec

type PatchSpec struct {
	BaseField

	// Value for the renderer
	//
	// 	say we have a yaml list ([bar]) stored at https://example.com/dukkha.yaml
	//
	// 		foo@http!:
	// 		  value: https://example.com/dukkha.yaml
	// 		  merge: [foo]
	//
	// then the resolve value of foo will be [bar, foo]
	Value *alterInterface `yaml:"value"`

	// Merge additional data into Value
	Merge []MergeSource `yaml:"merge,omitempty"`

	// Patches Value using standard rfc6902 json-patch
	Patches []JSONPatchSpec `yaml:"patches"`

	// Unique to make sure elements in the sequence is unique
	//
	// only effective when Value is yaml sequence
	Unique bool `yaml:"unique"`

	// MapListItemUnique to ensure items are unique in all merged lists respectively
	// lists with no merge data input are untouched
	MapListItemUnique bool `yaml:"map_list_item_unique"`

	// MapListAppend to append lists instead of replacing existing list
	MapListAppend bool `yaml:"map_list_append"`
}

func (*PatchSpec) ApplyTo

func (s *PatchSpec) ApplyTo(yamlData []byte) ([]byte, error)

Apply Merge and Patch to Value, Unique is ensured if set to true

type RenderingHandler

type RenderingHandler interface {
	// RenderYaml using specified renderer
	RenderYaml(renderer string, rawData interface{}) (result []byte, err error)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL