Documentation ¶
Index ¶
- Variables
- type Codec
- type DirNodeBase
- type FieldInfo
- type FileMarshaler
- type FileNode
- func (n *FileNode) ChildPathName(child Node, key, val reflect.Value) string
- func (FileNode) Detect(base NodeBase) error
- func (FileNode) New(base NodeBase, _ *Codec) (Node, error)
- func (n *FileNode) ReadTargets(c ReadContext, val Val) error
- func (n *FileNode) WriteTargets(c WriteContext, val Val) error
- type FileReader
- type FileTarget
- type FileTargets
- type FileTreeReader
- type FileWriter
- type JSONTag
- type MapNode
- type Node
- type NodeBase
- func (base NodeBase) FixedPathName() (string, bool)
- func (base NodeBase) ID() NodeID
- func (base NodeBase) NewKeyedVal(key reflect.Value) Val
- func (base NodeBase) NewKeyedValFrom(k, v reflect.Value) Val
- func (base NodeBase) NewVal() Val
- func (base NodeBase) NewValFrom(v reflect.Value) Val
- func (base NodeBase) PathName(val Val) string
- func (base NodeBase) Read(c ReadContext, val Val) error
- func (base NodeBase) Write(c WriteContext, val Val) error
- type NodeID
- type NodeSet
- type ReadContext
- type ReadTarget
- type SliceNode
- func (n *SliceNode) ChildPathName(child Node, key, val reflect.Value) string
- func (SliceNode) Detect(base NodeBase) error
- func (SliceNode) New(base NodeBase, c *Codec) (Node, error)
- func (n *SliceNode) ReadTargets(c ReadContext, val Val) error
- func (n *SliceNode) WriteTargets(c WriteContext, val Val) error
- type SpecialMapNode
- func (n *SpecialMapNode) ChildPathName(child Node, key, val reflect.Value) string
- func (SpecialMapNode) Detect(base NodeBase) error
- func (SpecialMapNode) New(base NodeBase, c *Codec) (Node, error)
- func (n *SpecialMapNode) ReadTargets(c ReadContext, val Val) error
- func (n *SpecialMapNode) WriteTargets(c WriteContext, val Val) error
- type StructNode
- func (n *StructNode) ChildPathName(child Node, key, val reflect.Value) string
- func (StructNode) Detect(base NodeBase) error
- func (StructNode) New(base NodeBase, c *Codec) (Node, error)
- func (n *StructNode) ReadTargets(c ReadContext, val Val) error
- func (n *StructNode) WriteTargets(c WriteContext, val Val) error
- type Tag
- type Val
- func (v Val) Append(elem Val)
- func (v Val) Final() reflect.Value
- func (v Val) FinalInterface() interface{}
- func (v Val) GetField(name string) reflect.Value
- func (v Val) Interface(filter func(interface{}) bool) (interface{}, bool)
- func (v Val) IsZero() bool
- func (v Val) MapElements(elemNode Node) []Val
- func (v Val) Method(name string) reflect.Value
- func (v Val) SetField(name string, val Val)
- func (v Val) SetMapElement(val Val)
- func (v Val) ShouldWrite() bool
- func (v Val) SliceElements(elemNode Node) []Val
- type WriteContext
- type WriteTarget
Constants ¶
This section is empty.
Variables ¶
var JSONWriter = FileMarshaler{ MarshalFunc: json.Marshal, UnmarshalFunc: json.Unmarshal, FileExtension: "json", RootFileName: "_", }
JSONWriter is a FileWriter configured to marshal JSON.
var NodeTypes = []Node{ &SpecialMapNode{}, &StructNode{}, &FileNode{}, &MapNode{}, &SliceNode{}, }
NodeTypes contains the set of nodes types in order of preference. Earlier types will be detected before later ones.
Functions ¶
This section is empty.
Types ¶
type Codec ¶
type Codec struct { // MarshalFunc is used to write file data. The signature matches // json.Marshal as well as many other standard marshalers. MarshalFunc func(interface{}) ([]byte, error) // UnmarshalFunc is used to read file data. The signature matches // json.Unmarshal as well as many other standard unmarshalers. UnmarshalFunc func([]byte, interface{}) error // FileExtension is the extension to use for reading and writing files. It // must not be empty. FileExtension, RootFileName string // contains filtered or unexported fields }
Codec provides the primary encoding and decoding facility of this package. Codecs should be re-used to take advantage of their caching of reflection data.
type DirNodeBase ¶
A DirNodeBase is the base type for a node stored in a directory.
func (*DirNodeBase) AnalyseElemNode ¶
func (n *DirNodeBase) AnalyseElemNode(parent Node, c *Codec) error
AnalyseElemNode sets the element node for this directory-bound node.
type FieldInfo ¶
type FieldInfo struct {
Name, FieldName, PathName, KeyField, GetKeyName, SetKeyName string
// Type is the type of this fields.
Type,
KeyType,
ElemType reflect.Type
// GetKeyFunc is a function getting the key from this map or slice's element.
GetKeyFunc,
SetKeyFunc reflect.Value
// Tag is the parsed hy tag.
Tag Tag
// Ignore indicates this field should not be written or read by hy.
Ignore,
IsField,
IsString,
AutoFieldName,
IsDir,
AutoPathName,
OmitEmpty bool
}
FieldInfo is information about a field.
func NewFieldInfo ¶
func NewFieldInfo(f reflect.StructField) (*FieldInfo, error)
NewFieldInfo creates a new FieldInfo, analysing the tag and checking the tag's named ID field or ID get/set methods for consistency.
type FileMarshaler ¶
type FileMarshaler struct { // MarshalFunc is called to marshal values to bytes. MarshalFunc func(interface{}) ([]byte, error) // UnmarshalFunc is called to matshal bytes to values. UnmarshalFunc func([]byte, interface{}) error // FileExtension is the extension of files and should correspond to the byte // format written and read by MarshalFunc and UnmarshalFunc. FileExtension, RootFileName string }
FileMarshaler knows how to turn FileTargets into real files.
func (FileMarshaler) ReadFile ¶
func (fm FileMarshaler) ReadFile(prefix, filePath string, v interface{}) error
ReadFile reads a file at prefix + t.Path into v.
func (FileMarshaler) WriteFile ¶
func (fm FileMarshaler) WriteFile(prefix string, t WriteTarget) error
WriteFile writes a file based on t.
type FileNode ¶
type FileNode struct {
NodeBase
}
A FileNode represents a node to be stored in a file.
func (*FileNode) ChildPathName ¶
ChildPathName returns an empty string (file targets don't have children).
func (*FileNode) ReadTargets ¶
func (n *FileNode) ReadTargets(c ReadContext, val Val) error
ReadTargets reads a single file target.
func (*FileNode) WriteTargets ¶
func (n *FileNode) WriteTargets(c WriteContext, val Val) error
WriteTargets returns the write target for this file.
type FileReader ¶
FileReader reads data from prefix + target.Path() into target.Data.
type FileTarget ¶
type FileTarget struct { FilePath string Value interface{} }
FileTarget represents a target file to be written.
type FileTargets ¶
type FileTargets struct {
// contains filtered or unexported fields
}
FileTargets is a map of file targets.
func MakeFileTargets ¶
func MakeFileTargets(capacity int) FileTargets
MakeFileTargets creates a new FileTargets with a starting capacity.
func MustNewFileTargets ¶
func MustNewFileTargets(targets ...*FileTarget) FileTargets
MustNewFileTargets wraps NewFileTargets and panics if it returns an error.
func NewFileTargets ¶
func NewFileTargets(targets ...*FileTarget) (FileTargets, error)
NewFileTargets creates a new FileTargets.
func (FileTargets) Add ¶
func (fts FileTargets) Add(targets ...*FileTarget) error
Add adds any number of file targets to this one. Returns an error if any of them share a path.
func (FileTargets) AddAll ¶
func (fts FileTargets) AddAll(other FileTargets) error
AddAll adds the contents of another FileTargets to this one. Returns an error if any of them share a path.
func (FileTargets) Paths ¶
func (fts FileTargets) Paths() []string
Paths returns the paths of all file targets.
func (FileTargets) Snapshot ¶
func (fts FileTargets) Snapshot() map[string]*FileTarget
Snapshot returns the map this FileTargets represents.
type FileTreeReader ¶
type FileTreeReader struct { // FileExtension is the extension of files to consider. FileExtension string // Prefix is the path prefix. Prefix string // RootFileName is the root file name. RootFileName string }
FileTreeReader gets targets from the filesystem.
func NewFileTreeReader ¶
func NewFileTreeReader(ext, rootFileName string) *FileTreeReader
NewFileTreeReader returns a new FileTreeReader configured to consider files with extension ext.
func (*FileTreeReader) MakeWalkFunc ¶
func (ftr *FileTreeReader) MakeWalkFunc(targets FileTargets) filepath.WalkFunc
MakeWalkFunc makes a func to process a single filesystem object.
func (*FileTreeReader) ReadTree ¶
func (ftr *FileTreeReader) ReadTree(prefix string) (FileTargets, error)
ReadTree reads a tree rooted at prefix and generates a target from each file with extension FileExtension found in the tree.
type FileWriter ¶
type FileWriter interface { // WriteFile writes a file representing target, by joining prefix with // Target.Path() WriteFile(prefix string, target WriteTarget) error }
FileWriter is something that can write Targets as files.
type JSONTag ¶
JSONTag represents a json field tag.
func ParseJSONTag ¶
func ParseJSONTag(field reflect.StructField) JSONTag
ParseJSONTag parses a json field tag from a struct field. Only strings, floats, integers, and booleans can be quoted.
type MapNode ¶
type MapNode struct { *DirNodeBase KeyType reflect.Type // MarshalKey gets a string from the key. MarshalKey func(key Val) string // UnmarshalKey sets a key from a string. UnmarshalKey func(key string, val reflect.Value) error }
A MapNode represents a map node to be stored in a directory.
func (*MapNode) ChildPathName ¶
ChildPathName returns the key as a string.
func (*MapNode) ReadTargets ¶
func (n *MapNode) ReadTargets(c ReadContext, val Val) error
ReadTargets reads targets into map entries.
func (*MapNode) WriteTargets ¶
func (n *MapNode) WriteTargets(c WriteContext, val Val) error
WriteTargets writes all map elements.
type Node ¶
type Node interface { // Detect returns nil if this node can handle this base type. Detect(NodeBase) error // New returns a new instance of a node. New(NodeBase, *Codec) (Node, error) // ID returns this node's ID. ID() NodeID // FixedPathName returns the indubitable path segment name of this node. FixedPathName() (string, bool) // ChildPathName returns the path segment for children of this node. // If the node's parent is a map or slice, both key and val will have // valid values, with val having the same type as this node. // If the node's parent is a map, then the key will be a value of the // parent's key type. // If the node's parent is a slice, then key will be an int value // representing the index of this element. // If the node's parent is a struct, then key will be an invalid value, // and val will be the value of that struct field. ChildPathName(child Node, key, val reflect.Value) string // PathName returns the path name of this node. Implemented in NodeBase. PathName(Val) string // WriteTargets writes file targets for this node to the context. WriteTargets(c WriteContext, val Val) error // Write writes file targets for this node to the context by first ensuring // val is not a pointer and then calling WriteTargets. Write(c WriteContext, val Val) error // Read wraps ReadTargets and takes care of pointers. Read(c ReadContext, val Val) error // ReadTargets reads key from contexts and returns its value. ReadTargets(c ReadContext, val Val) error NewVal() Val NewValFrom(reflect.Value) Val NewKeyedVal(key reflect.Value) Val NewKeyedValFrom(key, val reflect.Value) Val }
Node represents a generic node in the structure.
type NodeBase ¶
type NodeBase struct { NodeID // Parent is the parent of this node. It is nil only for the root node. Parent Node // FieldInfo is the field info for this node. Field *FieldInfo // Zero is a zero value of this node's Type. Zero interface{} // HasKey indicates if this type has a key (e.g. maps and slices) HasKey bool // Kind is the kind of NodeID.Type. Kind reflect.Kind // contains filtered or unexported fields }
NodeBase is a node in an analysis.
func NewNodeBase ¶
NewNodeBase returns a new NodeBase.
func (NodeBase) FixedPathName ¶
FixedPathName returns the fixed path name of this node. If there is no fixed path name, returns empty string and false. Otherwise returns the fixed path name and true.
func (NodeBase) NewKeyedVal ¶
NewKeyedVal is similar to NewVal but adds an associated key.
func (NodeBase) NewKeyedValFrom ¶
NewKeyedValFrom is similar to NewValFrom but adds an associated key.
func (NodeBase) NewValFrom ¶
NewValFrom creates a Val from an existing value.
type NodeID ¶
type NodeID struct { // ParentType is the type of this node's parent. ParentType, Type reflect.Type // IsPtr indicates if OwnType is a pointer really. IsPtr bool // FieldName is the name of the parent field containing this node. FieldName // will be empty unless ParentType is a struct. FieldName string }
NodeID identifies a node in the tree.
type NodeSet ¶
type NodeSet struct {
// contains filtered or unexported fields
}
NodeSet is a set of Node pointers indexed by ID.
type ReadContext ¶
type ReadContext struct { // Reader reads data from path. Reader FileReader // Parent is the parent read context. Parent *ReadContext // PathName is the name of this section of the path. PathName string // Prefix is the path prefix. Prefix string // contains filtered or unexported fields }
ReadContext is context collected during a read opration.
func NewReadContext ¶
func NewReadContext(prefix string, targets FileTargets, reader FileReader) ReadContext
NewReadContext returns a new read context.
func (ReadContext) Exists ¶
func (c ReadContext) Exists() bool
Exists checks that a file exists at the current path.
func (ReadContext) List ¶
func (c ReadContext) List() []string
List lists files in the current directory. TODO: This is horrible, need a tree file structure for targets.
func (ReadContext) Push ¶
func (c ReadContext) Push(pathName string) ReadContext
Push creates a derivative node context.
func (ReadContext) Read ¶
func (c ReadContext) Read(v interface{}) error
type ReadTarget ¶
type ReadTarget interface { // Path is the path where this target is stored. Path() string }
ReadTarget represents an input target, typically a file.
type SliceNode ¶
type SliceNode struct {
*DirNodeBase
}
A SliceNode represents a slice to be stored in a directory.
func (*SliceNode) ChildPathName ¶
ChildPathName returns the slice index as a string.
func (*SliceNode) ReadTargets ¶
func (n *SliceNode) ReadTargets(c ReadContext, val Val) error
ReadTargets reads targets into slice indicies.
func (*SliceNode) WriteTargets ¶
func (n *SliceNode) WriteTargets(c WriteContext, val Val) error
WriteTargets writes all the elements of the slice.
type SpecialMapNode ¶
type SpecialMapNode struct { NodeBase Map *MapNode GetAll func(Val) (reflect.Value, error) SetAll func(on Val, to reflect.Value) error }
SpecialMapNode represents a struct node with GetAll and SetAll methods.
func (*SpecialMapNode) ChildPathName ¶
func (n *SpecialMapNode) ChildPathName(child Node, key, val reflect.Value) string
ChildPathName delegates to MapNode.
func (SpecialMapNode) Detect ¶
func (SpecialMapNode) Detect(base NodeBase) error
Detect returns nil if base is a struct with appropriate GetAll and SetAll methods.
func (SpecialMapNode) New ¶
func (SpecialMapNode) New(base NodeBase, c *Codec) (Node, error)
New returns a new SpecialMapNode.
func (*SpecialMapNode) ReadTargets ¶
func (n *SpecialMapNode) ReadTargets(c ReadContext, val Val) error
ReadTargets delegates to MapNode.
func (*SpecialMapNode) WriteTargets ¶
func (n *SpecialMapNode) WriteTargets(c WriteContext, val Val) error
WriteTargets delegates to MapNode.
type StructNode ¶
type StructNode struct { FileNode // Fields is a map of simple struct field names to their types. Fields map[string]reflect.Type // Children is a map of field named to node pointers. Children map[string]*Node }
StructNode represents a struct to be stored in a file.
func (*StructNode) ChildPathName ¶
func (n *StructNode) ChildPathName(child Node, key, val reflect.Value) string
ChildPathName returns the path segment for this node's children.
func (StructNode) Detect ¶
func (StructNode) Detect(base NodeBase) error
Detect returns nil if this base is a struct.
func (StructNode) New ¶
func (StructNode) New(base NodeBase, c *Codec) (Node, error)
New creates a new StructNode.
func (*StructNode) ReadTargets ¶
func (n *StructNode) ReadTargets(c ReadContext, val Val) error
ReadTargets reads targets into struct fields.
func (*StructNode) WriteTargets ¶
func (n *StructNode) WriteTargets(c WriteContext, val Val) error
WriteTargets generates file targets.
type Val ¶
type Val struct { Base *NodeBase // Ptr is the underlying pointer value. Ptr reflect.Value // Key is the associated key for this value. May be invalid. Key reflect.Value // IsPtr indicates whether the final version of this value should be a // pointer. IsPtr bool }
Val wraps a reflect.Value so other code does not need to be aware of pointer wrapping and unwrapping.
func NewFreeValFrom ¶
NewFreeValFrom creates a new free value (not attached to any node).
func (Val) FinalInterface ¶
func (v Val) FinalInterface() interface{}
FinalInterface returns the true final value as an interface{}.
func (Val) Interface ¶
Interface selects the pointer or non-pointer version of this val that satisfies the filter function. It returns nil, false if none pass the filter.
func (Val) MapElements ¶
MapElements returns a slice of Val representing key, value pairs from this map. It panics if v does not represent a map.
func (Val) SetField ¶
SetField sets a field on this struct value. It panics if this value is not a struct.
func (Val) SetMapElement ¶
SetMapElement sets a map element using key and value from val. It panics if this value is not a map with corresponding key and value types.
func (Val) ShouldWrite ¶
ShouldWrite returns true if this value is not zero or if it does have a key. Zero unkeyed values are never written.
func (Val) SliceElements ¶
SliceElements returns a slice of Vals representing the index, value pairs from this slice. It panics if v is not a slice.
type WriteContext ¶
type WriteContext struct { // Parent is the parent write context. Parent *WriteContext // PathName is the name of this section of the path. PathName string // contains filtered or unexported fields }
WriteContext is context collected during a write opration.
func NewWriteContext ¶
func NewWriteContext() WriteContext
NewWriteContext returns a new write context.
func (WriteContext) Path ¶
func (c WriteContext) Path() string
Path returns the path of this context.
func (WriteContext) Push ¶
func (c WriteContext) Push(pathName string) WriteContext
Push creates a derivative node context.
func (WriteContext) SetRawValue ¶
func (c WriteContext) SetRawValue(v interface{}) error
SetRawValue sets the raw value of the current path.
func (WriteContext) SetValue ¶
func (c WriteContext) SetValue(val Val) error
SetValue sets the value of the current path.
type WriteTarget ¶
type WriteTarget interface { // Path is the path where this target is stored. Path() string // Data is the go value to be stored. Data() interface{} }
WriteTarget represents an output target, typically a file.