rs

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2021 License: Apache-2.0 Imports: 10 Imported by: 38

README

rs

CI PkgGoDev GoReportCard Coverage

Rendering Suffix Support for yaml

Usage

  1. Embed rs.BaseField as the very first field in your struct

    type MyStruct struct {
      rs.BaseField // MUST be the first one
    }
    
  2. Write yaml doc with rendering suffix (say my-renderer)

    foo@my-renderer: bar
    
  3. Implement the my-renderer to comply interface rs.RenderingHandler requirments (say MyRendererImpl)

  4. Unmarshal your yaml data using go-yaml to MyStruct object (say myStructObj)

  5. Resolve your yaml data by calling myStructObj.ResolveFields(&MyRendererImpl{}, -1)

NOTE: You can find more source code usage examples in arhat-dev/dukkha

Known Limitations

  • Inline field marshaling issue, see go-yaml/yaml#362

    • Cause: rs.BaseField implemented yaml.UnmarshalYAML

    Sample Code:

    type A struct{
      rs.BaseField
    }
    
    type B struct{
      rs.BaseField
    
      // UseA will not be marshaled
      UseA A `yaml:",inline"`
    }
    
  • Built-in map data structure with rendering suffix applied to map key are not supported

    Sample Code:

    type Bar struct {
      // ... any kind of fields
    }
    
    type Foo struct {
      rs.BaseField
    
      SomeBar map[string]Bar `yaml:"some_bar"`
    }
    
    some_bar:
      # this will not be resolved
      some_key_for_bar@my-renderer: my-renderer-value
    

    Workaround: Define your own struct, embed rs.BaseField as the first field and make the map a field with rs:"other" tag

    type BarMap struct{
      rs.BaseField
    
      Data map[string]Bar `rs:"other"`
    }
    
    type Foo struct {
      rs.BaseField
    
      SomeBar BarMap `yaml:"some_bar"`
    }
    

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)

func UniqueList added in v0.1.1

func UniqueList(dt []interface{}) []interface{}

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 {
	rs.BaseField // or *rs.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 *AnyObject `yaml:"value,omitempty" json:"value,omitempty"`

	// Select part of the value for patching
	//
	// this action happens before patching
	Select string `yaml:"select" json:"-"`
}

JSONPatchSpec per rfc6902

type MergeSource

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

	// Value for the source
	Value *AnyObject `yaml:"value,omitempty"`

	// Select some data from the source
	Select string `yaml:"select,omitempty"`
}

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