Documentation ¶
Index ¶
- Variables
- func NewMultiError(errs ...error) error
- func StringToRolesMap(m map[string][]role.Role) map[nodes.Value]ArrayOp
- type ArrayOp
- type ByFieldName
- type CodeTransformer
- type Field
- type FieldDesc
- type FieldDescs
- type FieldRole
- type FieldRoles
- type Fields
- func (o Fields) Check(st *State, n nodes.Node) (_ bool, gerr error)
- func (o Fields) CheckObj(st *State, n nodes.Object) (bool, error)
- func (o Fields) Construct(st *State, n nodes.Node) (nodes.Node, error)
- func (o Fields) ConstructObj(st *State, obj nodes.Object) (nodes.Object, error)
- func (o Fields) Fields() (FieldDescs, bool)
- func (Fields) Kinds() nodes.Kind
- type Has
- type Mapping
- type MappingOp
- type Mod
- type MultiError
- type Obj
- func (o Obj) Check(st *State, n nodes.Node) (bool, error)
- func (o Obj) CheckObj(st *State, n nodes.Object) (bool, error)
- func (o Obj) Construct(st *State, n nodes.Node) (nodes.Node, error)
- func (o Obj) ConstructObj(st *State, n nodes.Object) (nodes.Object, error)
- func (o Obj) Fields() (FieldDescs, bool)
- func (Obj) Kinds() nodes.Kind
- type ObjMap
- type ObjMapping
- func AnnotateType(typ string, fields ObjMapping, roles ...role.Role) ObjMapping
- func AnnotateTypeCustom(typ string, fields ObjMapping, rop ArrayOp, roles ...role.Role) ObjMapping
- func AnnotateTypeCustomMap(typ string, fields ObjMapping, fnc RolesByType, rop ArrayOp, ...) ObjMapping
- func MapObj(src, dst ObjectOp) ObjMapping
- func MapPart(vr string, m ObjMapping) ObjMapping
- func MapSemantic(nativeType string, semType interface{}, m ObjMapping) ObjMapping
- func MapSemanticPos(nativeType string, semType interface{}, pos map[string]string, m ObjMapping) ObjMapping
- func ObjScope(name string, m ObjMapping) ObjMapping
- type ObjRoles
- type ObjectOp
- func ASTObjectLeft(typ string, ast ObjectOp) ObjectOp
- func ASTObjectRight(typ string, norm ObjectOp, rop ArrayOp, roles ...role.Role) ObjectOp
- func ASTObjectRightCustom(typ string, norm ObjectOp, fnc RolesByType, rop ArrayOp, roles ...role.Role) ObjectOp
- func CasesObj(vr string, common ObjectOp, cases ObjectOps) ObjectOp
- func CommentNode(block bool, vr string, pos Op) ObjectOp
- func JoinObj(ops ...ObjectOp) ObjectOp
- func ObjOpScope(name string, op ObjectOp) ObjectOp
- func Operator(vr string, lookup map[nodes.Value]ArrayOp, roles ...role.Role) ObjectOp
- func Part(vr string, o ObjectOp) ObjectOp
- func UASTType(uobj interface{}, op ObjectOp) ObjectOp
- func UASTTypePart(vr string, uobj interface{}, op ObjectOp) ObjectOp
- type ObjectOps
- type ObjectToNode
- type Objects
- type Objs
- type Op
- func AnyNode(create Mod) Op
- func AnyVal(val nodes.Value) Op
- func Append(to Op, items ...ArrayOp) Op
- func ArrWith(arr Op, items ...Op) Op
- func Cases(vr string, cases ...Op) Op
- func Check(s Sel, op Op) Op
- func CommentText(tokens [2]string, vr string) Op
- func Each(vr string, op Op) Op
- func EachObjectRoles(vr string, roles ...role.Role) Op
- func EachObjectRolesByType(vr string, types map[string][]role.Role, roles ...role.Role) Op
- func EmptyObj() Op
- func If(cond string, then, els Op) Op
- func Lookup(op Op, m map[nodes.Value]nodes.Value) Op
- func LookupOpVar(vr string, cases map[nodes.Value]Op) Op
- func LookupVar(vr string, m map[nodes.Value]nodes.Value) Op
- func NotEmpty(op Op) Op
- func ObjectRoles(vr string, roles ...role.Role) Op
- func ObjectRolesCustom(vr string, obj ObjectOp, roles ...role.Role) Op
- func ObjectRolesCustomOp(vr string, obj ObjectOp, rop ArrayOp, roles ...role.Role) Op
- func OpScope(name string, op Op) Op
- func Opt(exists string, op Op) Op
- func OptObjectRoles(vr string, roles ...role.Role) Op
- func PrependOne(first Op, arr Op) Op
- func Quote(op Op) Op
- func SavePosLineCol(varLine, varCol string) Op
- func SavePosOffset(vr string) Op
- func Seq(ops ...Op) Op
- func StringConv(on Op, conv, rev StringFunc) Op
- func TypedObj(typ string, ops map[string]Op) Op
- func Uncomment(vr string, tokens [2]string) Op
- func UncommentCLike(vr string) Op
- func ValueConv(on Op, conv, rev ValueFunc) Op
- func VarKind(name string, k nodes.Kind) Op
- type ResponseMetadata
- type RolesByType
- type Sel
- type State
- func (st *State) ApplyFrom(st2 *State)
- func (st *State) Clone() *State
- func (st *State) GetStateVar(name string) ([]*State, bool)
- func (st *State) GetVar(name string) (nodes.Node, bool)
- func (st *State) MustGetVar(name string) (nodes.Node, error)
- func (st *State) MustGetVars(vars VarsPtrs) error
- func (st *State) Reset()
- func (st *State) SetStateVar(name string, sub []*State) error
- func (st *State) SetVar(name string, val nodes.Node) error
- func (st *State) SetVars(vars Vars) error
- type StringFunc
- type TransformFunc
- type TransformObjFunc
- type Transformer
- type ValueFunc
- type Vars
- type VarsPtrs
Constants ¶
This section is empty.
Variables ¶
var ( // ErrVariableRedeclared is returned when a transformation defines the same variable twice. ErrVariableRedeclared = errors.NewKind("variable %q redeclared (%v vs %v)") // ErrVariableNotDefined is returned when an operation excepts a variable to be defined, but it was not set // in current transformation state. // // Receiving this error might mean that transformations have an incorrect order and tries to use a variable // in Lookup or If, for example, before another transformation step that sets this variable is executed. // If this is the case, see Fields vs Obj comparison in regards to execution order. ErrVariableNotDefined = errors.NewKind("variable %q is not defined") // ErrExpectedObject is returned when transformation expected an object in the tree or variable, but got other type. ErrExpectedObject = errors.NewKind("expected object, got %T") // ErrExpectedList is returned when transformation expected an array in the tree or variable, but got other type. ErrExpectedList = errors.NewKind("expected list, got %T") // ErrExpectedValue is returned when transformation expected a value in the tree or variable, but got other type. ErrExpectedValue = errors.NewKind("expected value, got %T") // ErrUnhandledValueIn is returned when Lookup fails to find a value in the map. It's recommended to define all // values in the lookup map. If it's not possible, use nil value as a key to set default case for Lookup. ErrUnhandledValueIn = errors.NewKind("unhandled value: %v in %v") // ErrUnexpectedNode is an internal error returned by SDK when a transformation that creates a value receives another // value as an argument. This should not happen. ErrUnexpectedNode = errors.NewKind("expected node to be nil, got: %v") // ErrUnexpectedValue is returned when the value from state does not match constraints enforced by Construct. ErrUnexpectedValue = errors.NewKind("unexpected value: %v") // ErrUnexpectedType is returned in both Check and Construct when unexpected type is received as an argument or variable. ErrUnexpectedType = errors.NewKind("unexpected type: exp %T vs got %T") // ErrAmbiguousValue is returned when Lookup map contains the same value mapped to different keys. ErrAmbiguousValue = errors.NewKind("map has ambiguous value %v") // ErrUnusedField is returned when a transformation is not defined as partial, but does not process a specific key // found in object. This usually means that an AST has a field that is not covered by transformation code and it // should be added to the mapping. ErrUnusedField = errors.NewKind("field was not used: %v") // ErrDuplicateField is returned when trying to create a Fields definition with two items with the same name. ErrDuplicateField = errors.NewKind("duplicate field: %v") // ErrUndefinedField is returned when trying to create an object with a field that is not defined in the type spec. ErrUndefinedField = errors.NewKind("undefined field: %v") )
Functions ¶
func NewMultiError ¶
NewMultiError creates an error that contains multiple other errors. If a slice is empty, it returns nil. If there is only one error, it is returned directly.
Types ¶
type ArrayOp ¶
type ArrayOp interface { Op // contains filtered or unexported methods }
ArrayOp is a subset of operations that operates on an arrays with a pre-defined size. See Arr.
func AppendArr ¶
AppendArr asserts that a node is a Array and checks that it contains a defined set of nodes at the end. Reversal uses sub-operation to create a Array and appends provided element lists at the end of it.
func AppendRoles ¶
AppendRoles can be used to append more roles to an output of a specific operation.
func Arr ¶
Arr checks if the current object is a list with a number of elements matching a number of ops, and applies ops to corresponding elements. Reversal creates a list of the size that matches the number of ops and creates each element with the corresponding op.
func LookupArrOpVar ¶
LookupArrOpVar is like LookupOpVar but returns an array operation. Default value can be specified by setting the nil key.
type ByFieldName ¶
type ByFieldName []Field
ByFieldName will sort fields descriptions by their names.
func (ByFieldName) Len ¶
func (arr ByFieldName) Len() int
func (ByFieldName) Less ¶
func (arr ByFieldName) Less(i, j int) bool
func (ByFieldName) Swap ¶
func (arr ByFieldName) Swap(i, j int)
type CodeTransformer ¶
type CodeTransformer interface {
OnCode(code string) Transformer
}
CodeTransformer is a special case of Transformer that needs an original source code to operate.
type Field ¶
type Field struct { Name string // name of the field // Optional can be set to make a field optional. Provided string is used as a variable name to the state of the field. // Note that "optional" means that the field may not exists in the object, and it does not mean that the field can be nil. // To handle nil fields, see Opt operation. Optional string Op Op // operation used to check/construct the field value }
Field is an operation on a specific field of an object.
func RolesField ¶
RolesField will create a roles field that appends provided roles to existing ones. In case no roles are provided, it will save existing roles, if any.
type FieldDescs ¶
FieldDescs is a descriptions of static fields of an object. Transformation operation may return this struct to indicate what fields they will require.
func (FieldDescs) CheckObj ¶
func (f FieldDescs) CheckObj(n nodes.Object) bool
CheckObj verifies that an object matches field descriptions. It ignores all fields in the object that are not described.
func (FieldDescs) Clone ¶
func (f FieldDescs) Clone() FieldDescs
Clone makes a copy of field description, without cloning each field values.
type FieldRole ¶
type FieldRole struct { Rename string // rename the field to this name in resulting tree Skip bool // omit this field in the resulting tree Add bool // create this field in the resulting tree Opt bool // field can be nil Arr bool // field is an array; apply roles or custom operation to each element Sub ObjMapping // a mapping that will be used for this field; overrides Op Op Op // use this operation for the field on both sides of transformation Roles []role.Role // list of roles to append to the field; has no effect if Op is set }
FieldRole is a list of operations that can be applied to an object field.
type FieldRoles ¶
FieldRoles is a helper type that stores a mapping from field names to operations that needs to be applied to it.
func (FieldRoles) Mapping ¶
func (f FieldRoles) Mapping() (src, dst Op)
func (FieldRoles) ObjMapping ¶
func (f FieldRoles) ObjMapping() (src, dst ObjectOp)
type Fields ¶
type Fields []Field
Fields verifies that current node is an object and checks its fields with a defined operations. If field does not exist, object will be skipped. Reversal changes node type to object and creates all fields with a specified operations. Implementation will track a list of unprocessed object keys and will return an error in case the field was not used. To preserve all unprocessed keys use Part.
func (Fields) Check ¶
Check will verify that a node is an object and that fields matches a defined set of rules.
If Part transform was not used, this operation will also ensure that all fields in the object are covered by field descriptions. If Pre was used, all unknown fields will be saved and restored to a new object on Construct.
For information on optional fields see Field documentation.
func (Fields) CheckObj ¶
Check will verify that a node is an object and that fields matches a defined set of rules.
If Part transform was not used, this operation will also ensure that all fields in the object are covered by field descriptions.
For information on optional fields see Field documentation.
func (Fields) Construct ¶
Construct will create a new object and will populate it's fields according to field descriptions. If Part was used, it will also restore all unhandled fields.
func (Fields) ConstructObj ¶
ConstructObj will create a new object and will populate it's fields according to field descriptions.
func (Fields) Fields ¶
func (o Fields) Fields() (FieldDescs, bool)
type Has ¶
Has is a check-only operation that verifies that object has specific fields and they match given checks.
type Mapping ¶
type Mapping interface {
Mapping() (src, dst Op)
}
func AnnotateIfNoRoles ¶
AnnotateIfNoRoles adds roles to specific type if there were no roles set for it yet.
Since rules are applied depth-first, this operation will work properly only in a separate mapping step. In other cases it will apply itself before parent node appends field roles.
func Map ¶
Map creates a two-way mapping between two transform operations. The first operation will be used to check constraints for each node and store state, while the second one will use the state to construct a new tree.
type MappingOp ¶
func Is ¶
func Is(o interface{}) MappingOp
Is checks if the current node to a given node. It can be a value, array or an object. Reversal clones the provided value into the tree.
type Mod ¶
type Mod interface { // Construct will use variables stored in State to reconstruct an AST node. // Node that is provided as an argument may be used as a base for reconstruction. Construct(st *State, n nodes.Node) (nodes.Node, error) }
Mod is an operation that can reconstruct an AST node from a given State.
type MultiError ¶
type MultiError struct {
Errs []error
}
MultiError is an error that groups multiple other errors.
func (*MultiError) Error ¶
func (e *MultiError) Error() string
type Obj ¶
Obj is a helper for defining a transformation on an object fields. See Object. Operations will be sorted by the field name before execution.
func (Obj) Construct ¶
Construct will make an Object operation from this helper and call Construct on it.
func (Obj) ConstructObj ¶
ConstructObj will make an Object operation from this helper and call Construct on it.
func (Obj) Fields ¶
func (o Obj) Fields() (FieldDescs, bool)
type ObjMap ¶
func (ObjMap) ObjMapping ¶
type ObjMapping ¶
func AnnotateType ¶
func AnnotateType(typ string, fields ObjMapping, roles ...role.Role) ObjMapping
AnnotateType is a helper to assign roles to specific fields. All fields are assumed to be optional and should be objects.
func AnnotateTypeCustom ¶
func AnnotateTypeCustom(typ string, fields ObjMapping, rop ArrayOp, roles ...role.Role) ObjMapping
AnnotateTypeCustom is like AnnotateType but allows to specify custom roles operation as well as a mapper function.
func AnnotateTypeCustomMap ¶
func AnnotateTypeCustomMap(typ string, fields ObjMapping, fnc RolesByType, rop ArrayOp, roles ...role.Role) ObjMapping
AnnotateTypeCustomMap is like AnnotateTypeCustom, but allows to specify additional roles for each type.
func MapObj ¶
func MapObj(src, dst ObjectOp) ObjMapping
func MapPart ¶
func MapPart(vr string, m ObjMapping) ObjMapping
func MapSemantic ¶
func MapSemantic(nativeType string, semType interface{}, m ObjMapping) ObjMapping
func MapSemanticPos ¶
func MapSemanticPos(nativeType string, semType interface{}, pos map[string]string, m ObjMapping) ObjMapping
func ObjScope ¶
func ObjScope(name string, m ObjMapping) ObjMapping
type ObjRoles ¶
ObjRoles is a helper type that stores a mapping from field names to their roles.
func (ObjRoles) ObjMapping ¶
type ObjectOp ¶
type ObjectOp interface { Op // Fields returns a map of field names that will be processed by this operation. // The flag if the map indicates if the field is required. // False bool value returned as a second argument indicates that implementation will process all fields. Fields() (FieldDescs, bool) CheckObj(st *State, n nodes.Object) (bool, error) ConstructObj(st *State, n nodes.Object) (nodes.Object, error) }
ObjectOp is an operation that is executed on an object. See Object.
func ASTObjectLeft ¶
ASTObjectLeft construct a native AST shape for a given type name.
func ASTObjectRight ¶
ASTObjectRight constructs an annotated native AST shape with specific roles.
func ASTObjectRightCustom ¶
func ASTObjectRightCustom(typ string, norm ObjectOp, fnc RolesByType, rop ArrayOp, roles ...role.Role) ObjectOp
ASTObjectRightCustom is like ASTObjectRight but allow to specify additional roles for each type.
func ObjOpScope ¶
func Part ¶
Part defines a partial transformation of an object. All unused fields will be stored into variable with a specified name.
func UASTTypePart ¶
type ObjectToNode ¶
type ObjectToNode struct { // InternalTypeKey is the name of the key that the native AST uses // to differentiate the type of the AST nodes. This internal key will then be // checkable in the AnnotationRules with the `HasInternalType` predicate. This // field is mandatory. InternalTypeKey string // OffsetKey is the key used in the native AST to indicate the absolute offset, // from the file start position, where the code mapped to the AST node starts. OffsetKey string // EndOffsetKey is the key used in the native AST to indicate the absolute offset, // from the file start position, where the code mapped to the AST node ends. EndOffsetKey string // LineKey is the key used in the native AST to indicate // the line number where the code mapped to the AST node starts. LineKey string // EndLineKey is the key used in the native AST to indicate // the line number where the code mapped to the AST node ends. EndLineKey string // ColumnKey is a key that indicates the column inside the line ColumnKey string // EndColumnKey is a key that indicates the column inside the line where the node ends. EndColumnKey string }
ObjectToNode transform trees that are represented as nested JSON objects. That is, an interface{} containing maps, slices, strings and integers. It then converts from that structure to *Node.
func (ObjectToNode) Mapping ¶
func (n ObjectToNode) Mapping() Mapping
Mapping construct a transformation from ObjectToNode definition.
type Op ¶
Op is a generic AST transformation step that describes a shape of an AST tree. It can be used to either check the constraints for a specific node and populate state, or to reconstruct an AST shape from a the same state (probably produced by another Op).
func AnyNode ¶
AnyNode matches any node and throws it away. Reversal will create a node with create op.
func Append ¶
Append is like AppendArr but allows to set more complex first operation. Result of this operation should still be an array.
func Check ¶
Check tests first check-only operation before applying the main op. It won't use the check-only argument for Construct. The check-only operation will not be able to set any variables or change state by other means.
func CommentText ¶
func Each ¶
Each checks that current node is an array and applies sub-operation to each element. It uses a variable to store state of each element.
func EachObjectRoles ¶
EachObjectRoles is a helper that constructs Each(ObjectRoles(roles)) operation. It will annotate all elements of the array with a specified list of roles.
func EachObjectRolesByType ¶
EachObjectRolesByType is like EachObjectRoles but adds additional roles for each type specified in the map. EachObjectRolesByType should always be paired on both side of transform because it uses variables to store type info.
func Lookup ¶
Lookup uses a value of current node to find a replacement for it in the map and checks result with op. The reverse step will use a reverse map to lookup value created by op and will assign it to the current node. Since reversal transformation needs to build a reverse map, the mapping should not be ambiguous in reverse direction (no duplicate values).
func LookupOpVar ¶
LookupOpVar is a conditional branch that takes a value of a variable and checks the map to find an appropriate operation to apply to current node. Note that the variable must be defined prior to this transformation, thus You might need to use Pre to define a variable used in this condition.
func ObjectRoles ¶
ObjectRoles creates a shape that adds additional roles to an object. Should only be used in other object fields, since it does not set any type constraints. Specified variable name (vr) will be used as a prefix for variables to store old roles and unprocessed object keys.
func ObjectRolesCustom ¶
ObjectRolesCustom is like ObjectRoles but allows to apecify additional conatraints for object.
func ObjectRolesCustomOp ¶
ObjectRolesCustomOp is like ObjectRolesCustom but allows to apecify a custom roles lookup.
func OptObjectRoles ¶
OptObjectRoles is like ObjectRoles, but marks an object as optional.
func PrependOne ¶
PrependOne prepends a single element to an array.
func SavePosLineCol ¶
SavePosLineCol makes an operation that describes a uast.Position object with Line and Col field set to named variables.
func SavePosOffset ¶
SavePosOffset makes an operation that describes a uast.Position object with Offset field set to a named variable.
func Seq ¶
Seq checks current node with all ops in a sequence and fails if any of them fails. Reversal applies all modifications from ops to the current node. Typed ops should be at the beginning of the list to make sure that `Construct` creates a correct node type before applying specific changes to it.
func StringConv ¶
func StringConv(on Op, conv, rev StringFunc) Op
StringConv is like ValueConv, but only processes string arguments.
func TypedObj ¶
TypedObj is a shorthand for an object with a specific type and multiples operations on it.
func Uncomment ¶
Uncomment removes specified tokens from the beginning and from the end of a given string variable.
func UncommentCLike ¶
UncommentCLike removes // and /* */ symbols from a given string variable.
type ResponseMetadata ¶
type ResponseMetadata struct { // TopLevelIsRootNode tells ToNode where to find the root node of // the AST. If true, the root will be its input argument. If false, // the root will be the value of the only key present in its input // argument. TopLevelIsRootNode bool }
ResponseMetadata is a transformation that is applied to the root of AST tree to trim any metadata that might be there.
type RolesByType ¶
RolesByType is a function for getting roles for specific AST node type.
type Sel ¶
type Sel interface { // Kinds returns a mask of all nodes kinds that this operation might match. Kinds() nodes.Kind // Check will verify constraints for a single node and returns true if an objects matches them. // It can also populate the State with variables that can be used later to Construct a different object from the State. Check(st *State, n nodes.Node) (bool, error) }
Sel is an operation that can verify if a specific node matches a set of constraints or not.
type State ¶
type State struct {
// contains filtered or unexported fields
}
State stores all variables (placeholder values, flags and wny other state) between Check and Construct steps.
func NewState ¶
func NewState() *State
NewState creates a new state for Ops to work on. It stores variables, flags and anything that necessary for transformation steps to persist data.
func (*State) Clone ¶
Clone will return a copy of the State. This can be used to apply Check and throw away any variables produced by it. To merge a cloned state back use ApplyFrom on a parent state.
func (*State) GetStateVar ¶
GetStateVar returns a stored sub-state from a named variable.
func (*State) MustGetVar ¶
MustGetVar looks up a named variable and returns ErrVariableNotDefined in case it does not exists.
func (*State) MustGetVars ¶
MustGetVars is like MustGetVar but fetches multiple variables in one operation.
func (*State) Reset ¶
func (st *State) Reset()
Reset clears the state and allows to reuse an object.
func (*State) SetStateVar ¶
SetStateVar sets a sub-state variable. It returns ErrVariableRedeclared if the variable with this name already exists.
type StringFunc ¶
StringFunc is a function that transforms string values.
type TransformFunc ¶
TransformFunc is a function that will be applied to each AST node to transform the tree. It returns a new AST and true if tree was changed, or an old node and false if no modifications were done. The the tree will be traversed automatically and the callback will be called for each node.
func RolesDedup ¶
func RolesDedup() TransformFunc
RolesDedup is an irreversible transformation that removes duplicate roles from AST nodes.
type TransformObjFunc ¶
TransformObjFunc is like TransformFunc, but only matches Object nodes.
func (TransformObjFunc) Func ¶
func (f TransformObjFunc) Func() TransformFunc
Func converts this TransformObjFunc to a regular TransformFunc by skipping all non-object nodes.
type Transformer ¶
Transformer is an interface for transformations that operates on AST trees. An implementation is responsible for walking the tree and executing transformation on each AST node.
func DefaultNamespace ¶
func DefaultNamespace(ns string) Transformer
DefaultNamespace is a transform that sets a specified namespace for predicates and values that doesn't have a namespace.
func Mappings ¶
func Mappings(maps ...Mapping) Transformer
Mappings takes multiple mappings and optimizes the process of applying them as a single transformation.
func Transformers ¶
func Transformers(arr ...[]Transformer) []Transformer
Transformers appends all provided transformer slices into single one.