Documentation ¶
Overview ¶
Package dyml contains the Double Yielding Markup Language.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Unmarshal ¶
Unmarshal takes dyml input and parses it into the given struct. If "into" is not a struct or a pointer to a struct, this method will panic. As this uses go's reflect package, only exported names can be unmarshalled. Strict mode requires that all fields of the struct are set and defined exactly once. You can set struct tags to influence the unmarshalling process. All tags must have the form `dyml:"..."` and are a list of comma separated identifiers.
The first identifier can be used to rename the field, so that an element with the renamed name is parsed, and not the name of the struct field.
// This dyml snippet... #!{item{...}} // could be unmarshalled into this go struct. type Example struct { SomeName Content `dyml:"item"` }
The second identifier is used to specify what kind of thing is being parsed. This can be used to parse attributes (attr) or text (text).
Attributes can be parsed into primitive types: string, bool and the integer (signed & unsigned) and float types. Should the value not be valid for the target type, e.g. an integer that is too large or a negative value for an uint, an error is returned describing the issue.
// This dyml snippet... #item @key{value} @X{123} // could be unmarshalled into this go struct. type Example struct { SomeName string `dyml:"key,attr"` // Notice how you can rename an attribute X int `dyml:",attr"` // You can choose to not rename it, by omitting the rename parameter. }
'inner' can be used to parse elements that are the contents of the surrounding element. Consider this example to parse plain text without surrounding elements:
// This dyml snippet... #! { "hello" "more text" } // could be unmarshalled into this go struct. type Example struct { Something string `dyml:",inner"` }
When collecting text this way all text inside the node will be concatenated in non-strict mode ("hellomore text" in the above example). In strict mode exactly one text child is required. In the following example inner is used to parse a map-like Dyml definition into a map without a supporting element.
// This dyml snippet... #! { A "B" C "D" } // could be unmarshalled into this go struct. type Example struct { Something map[string]string `dyml:",inner"` }
dyml can unmarshal into maps. The map key must be a primitive type. The map value must be a primitive type, parser.TreeNode or *parser.TreeNode. Parsing maps will read first level elements as map keys and the first child of each as the map value. In strict mode the map key is required to have exactly one child. By specifying parser.TreeNode (or a pointer to it) as the value type you can access the raw tree that would be parsed as a value. This is useful if you want to have more control over the value for doing more complex manipulations than just parsing a primitive.
// This dyml snippet... #! { SomeMap { a 123, // Numbers are valid identifiers, so this works b "1.5" // but all other values should be enclosed in quotes. } } // could be unmarshalled into this go struct. type Example struct { SomeMap map[string]float64 }
dyml also supports unmarshalling slices. When no tag is specified in the struct, elements in dyml are unmarshalled into the slice directly. Should you specify a tag on the field in your struct, then only elements with that tag will be parsed. See the examples for more details.
Example ¶
type Animal struct { Name string `dyml:"name"` Age uint `dyml:"age"` } input := strings.NewReader("#name Gopher #age 3") var animal Animal Unmarshal(input, &animal, false) fmt.Printf("Hello %d year old %s!", animal.Age, animal.Name)
Output: Hello 3 year old Gopher !
Types ¶
type UnmarshalError ¶
type UnmarshalError struct { Node *parser.TreeNode Detail string // contains filtered or unexported fields }
UnmarshalError is an error that occurred during unmarshalling. It contains the offending node, a string with details and an underlying error (if any).
func NewUnmarshalError ¶
func NewUnmarshalError(node *parser.TreeNode, detail string, wrapping error) UnmarshalError
func (UnmarshalError) Error ¶
func (u UnmarshalError) Error() string
func (*UnmarshalError) Unwrap ¶
func (u *UnmarshalError) Unwrap() error
type Unmarshaler ¶
Unmarshaler can be implemented on a struct to define custom unmarshalling behavior.
Directories ¶
Path | Synopsis |
---|---|
Package parser contains the parser that transforms tokens generated by the lexer in package token to a tree representation.
|
Package parser contains the parser that transforms tokens generated by the lexer in package token to a tree representation. |
Package token contains the token and lexer logic.
|
Package token contains the token and lexer logic. |
gen
Generate token identifiers for all structs in parser2/token.go and place them in parser2/token.gen.go.
|
Generate token identifiers for all structs in parser2/token.go and place them in parser2/token.gen.go. |
Package util contains utility functions that may be useful for different parsers and encoders.
|
Package util contains utility functions that may be useful for different parsers and encoders. |