kmgYaml

package
v0.0.0-...-05317bf Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2015 License: MIT Imports: 15 Imported by: 0

README

kmgYaml

origin code come from "launchpad.net/goyaml"

add following bugfix:

  • improve: use github for source control.
  • improve: struct key name will not change when Unmarshal and Marshal by default(goyaml will change them to lowercase).
  • improve: chinese string will not Marshal to "\uxxxx"
  • bug:"1" can not unmarshal to float64 problem
  • improve: Can unmarshal array problem now.
  • improve: Can unmarshal/marshal time.Time correct.
  • bug: use "总数 :" as value of a map will generate a not valid yaml ,fixed in yaml_emitter_analyze_scalar():w = width(value[i]) //bug origin width(value[0])
  • improve: Can understand golang inline of struct like encoding/json

Documentation

Overview

Package goyaml implements YAML support for the Go language.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Json2YamlIo

func Json2YamlIo(r io.Reader, w io.Writer) error

先 Yaml2JsonIo , 后 Json2YamlIo 可能不会获得完全一致的结果

func Marshal

func Marshal(in interface{}) (out []byte, err error)

Marshal serializes the value provided into a YAML document. The structure of the generated document will reflect the structure of the value itself. Maps, pointers to structs and ints, etc, may all be used as the in value.

In the case of struct values, only exported fields will be serialized. The lowercased field name is used as the key for each exported field, but this behavior may be changed using the respective field tag. The tag may also contain flags to tweak the marshalling behavior for the field. Conflicting names result in a runtime error. The tag format accepted is:

`(...) yaml:"[<key>][,<flag1>[,<flag2>]]" (...)`

The following flags are currently supported:

omitempty    Only include the field if it's not set to the zero
             value for the type or to empty slices or maps.
             Does not apply to zero valued structs.

flow         Marshal using a flow style (useful for structs,
             sequences and maps.

inline       Inline the struct it's applied to, so its fields
             are processed as if they were part of the outer
             struct.

In addition, if the key is "-", the field is ignored.

For example:

type T struct {
    F int "a,omitempty"
    B int
}
goyaml.Marshal(&T{B: 2}) // Returns "b: 2\n"
goyaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n"

func MustMarshal

func MustMarshal(obj interface{}) []byte

func MustReadFile

func MustReadFile(path string, obj interface{})

func MustUnmarshal

func MustUnmarshal(in []byte, obj interface{})

func MustWriteFile

func MustWriteFile(path string, obj interface{})

func ReadFile

func ReadFile(path string, obj interface{}) error

func Unmarshal

func Unmarshal(in []byte, out interface{}) (err error)

Unmarshal decodes the first document found within the in byte slice and assigns decoded values into the object pointed by out.

Maps, pointers to structs and ints, etc, may all be used as out values. If an internal pointer within a struct is not initialized, goyaml will initialize it if necessary for unmarshalling the provided data, but the struct provided as out must not be a nil pointer.

The type of the decoded values and the type of out will be considered, and Unmarshal() will do the best possible job to unmarshal values appropriately. It is NOT considered an error, though, to skip values because they are not available in the decoded YAML, or if they are not compatible with the out value. To ensure something was properly unmarshaled use a map or compare against the previous value for the field (usually the zero value).

Struct fields are only unmarshalled if they are exported (have an upper case first letter), and will be unmarshalled using the field name lowercased by default. When custom field names are desired, the tag value may be used to tweak the name. Everything before the first comma in the field tag will be used as the name. The values following the comma are used to tweak the marshalling process (see Marshal). Conflicting names result in a runtime error.

For example:

type T struct {
    F int `yaml:"a,omitempty"`
    B int
}
var T t
goyaml.Unmarshal([]byte("a: 1\nb: 2"), &t)

See the documentation of Marshal for the format of tags and a list of supported tag options.

func WriteFile

func WriteFile(path string, obj interface{}) error

func Yaml2JsonIndentIo

func Yaml2JsonIndentIo(r io.Reader, w io.Writer) (err error)

func Yaml2JsonIo

func Yaml2JsonIo(r io.Reader, w io.Writer) (err error)

func Yaml2JsonTransformData

func Yaml2JsonTransformData(in interface{}) (out interface{}, err error)

Types

type Getter

type Getter interface {
	GetYAML() (tag string, value interface{})
}

Objects implementing the goyaml.Getter interface will get the GetYAML() method called when goyaml is requested to marshal the given value, and the result of this method will be marshaled in place of the actual object.

type Setter

type Setter interface {
	SetYAML(tag string, value interface{}) bool
}

Objects implementing the goyaml.Setter interface will receive the YAML tag and value via the SetYAML method during unmarshaling, rather than being implicitly assigned by the goyaml machinery. If setting the value works, the method should return true. If it returns false, the given value will be omitted from maps and slices.

Jump to

Keyboard shortcuts

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