transformer

package
v3.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2019 License: GPL-3.0 Imports: 10 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
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")
	// ErrVariableUnused is returned when a first half of transformation defines a variable and the second part
	// never uses it in any operations.
	//
	// If you receive this error and still think that every variable is used - double check conditional branches
	// like Opt, If, Case, etc.
	ErrVariableUnused = errors.NewKind("variables %q unused in the second part of the transform")
	// 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.
	//
	// Use NewErrUnusedField for constructing this error.
	ErrUnusedField = errors.NewKind("unused field(s) on node %v: %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 NewErrUnusedField

func NewErrUnusedField(n nodes.Object, fields []string) error

NewErrUnusedField is a helper for creating ErrUnusedField.

It will include a short type information for the node to simplify debugging.

func NewMultiError

func NewMultiError(errs ...error) error

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.

func StringToRolesMap

func StringToRolesMap(m map[string][]role.Role) map[nodes.Value]ArrayOp

StringToRolesMap is a helper to generate an array operation map that can be used for Lookup from a map from string values to roles.

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

func AppendArr(items ...ArrayOp) ArrayOp

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

func AppendRoles(old ArrayOp, roles ...role.Role) ArrayOp

AppendRoles can be used to append more roles to an output of a specific operation.

func Arr

func Arr(ops ...Op) ArrayOp

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

func LookupArrOpVar(vr string, cases map[nodes.Value]ArrayOp) ArrayOp

LookupArrOpVar is like LookupOpVar but returns an array operation. Default value can be specified by setting the nil key.

func One

func One(op Op) ArrayOp

One is a shorthand for a list with one element.

func Roles

func Roles(roles ...role.Role) ArrayOp

Roles makes an operation that will check/construct a list of roles.

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
	// Drop the field if it exists. Optional is implied, but the variable won't be created
	// in this case. Op should be set and it will be called to check the value before
	// dropping it. If the check fails, the whole transform will be canceled.
	//
	// Please note that you should avoid dropping fields with Any unless there is no
	// reasonable alternative.
	Drop bool
	Op   Op // operation used to check/construct the field value
}

Field is an operation on a specific field of an object.

func RolesField

func RolesField(vr string, roles ...role.Role) Field

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.

func RolesFieldOp

func RolesFieldOp(vr string, op ArrayOp, roles ...role.Role) Field

RolesFieldOp is like RolesField but allows to specify custom roles op to use.

func (Field) Desc

func (f Field) Desc() FieldDesc

Desc returns a field descriptor.

type FieldDesc

type FieldDesc struct {
	// Optional indicates that field might not exist in the object.
	Optional bool
	// Fixed is set if a field is required to have a specific value. The value may be nil.
	Fixed *nodes.Node
}

FieldDesc is a field descriptor for operations that act on objects.

It is used for transformation optimizer to filter candidate nodes upfront without running the full transformation tree.

func (*FieldDesc) SetValue

func (f *FieldDesc) SetValue(sel Sel)

SetValue checks the selector for a fixed value and sets it for the field descriptor.

type FieldDescs

type FieldDescs struct {
	// contains filtered or unexported fields
}

FieldDescs contains descriptions of static fields of an object.

Transformations may return this type to indicate what fields they will require.

See FieldDesc for details.

func NewFieldDescs

func NewFieldDescs(n int) FieldDescs

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.

func (*FieldDescs) Get

func (f *FieldDescs) Get(k string) (FieldDesc, bool)

Get the field descriptor by its name.

func (*FieldDescs) Has

func (f *FieldDescs) Has(k string) bool

Has checks if a field with a given name exists.

func (*FieldDescs) Index

func (f *FieldDescs) Index(i int) (FieldDesc, string)

Index return the field descriptor and its name, given an index.

func (*FieldDescs) Len

func (f *FieldDescs) Len() int

Len returns a number of fields.

func (*FieldDescs) Set

func (f *FieldDescs) Set(k string, d FieldDesc)

Set a field descriptor by name.

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

type FieldRoles map[string]FieldRole

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

func (o Fields) Check(st *State, n nodes.Node) (_ bool, gerr error)

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

func (o Fields) CheckObj(st *State, n nodes.Object) (bool, error)

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

func (o Fields) Construct(st *State, n nodes.Node) (nodes.Node, error)

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

func (o Fields) ConstructObj(st *State, obj nodes.Object) (nodes.Object, error)

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)

func (Fields) Kinds

func (Fields) Kinds() nodes.Kind

type Has

type Has map[string]Sel

Has is a check-only operation that verifies that object has specific fields and they match given checks.

func (Has) Check

func (m Has) Check(st *State, n nodes.Node) (bool, error)

Check verifies that specified fields exist and matches the provided sub-operations.

func (Has) CheckObj

func (m Has) CheckObj(st *State, n nodes.Object) (bool, error)

CheckObj verifies that specified fields exist and matches the provided sub-operations.

func (Has) Fields

func (m Has) Fields() (FieldDescs, bool)

func (Has) Kinds

func (Has) Kinds() nodes.Kind

type HasFields

type HasFields map[string]bool

HasFields is a check-only operation that verifies existence of specific fields.

func (HasFields) Check

func (m HasFields) Check(st *State, n nodes.Node) (bool, error)

Check verifies that specified fields exist and matches the provided sub-operations.

func (HasFields) CheckObj

func (m HasFields) CheckObj(st *State, n nodes.Object) (bool, error)

CheckObj verifies that specified fields exist and matches the provided sub-operations.

func (HasFields) Fields

func (m HasFields) Fields() (FieldDescs, bool)

func (HasFields) Kinds

func (HasFields) Kinds() nodes.Kind

type Mapping

type Mapping interface {
	Mapping() (src, dst Op)
}

func AnnotateIfNoRoles

func AnnotateIfNoRoles(typ string, roles ...role.Role) Mapping

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 Identity

func Identity(op Op) Mapping

func Map

func Map(src, dst Op) Mapping

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.

func MapEach

func MapEach(vr string, m Mapping) Mapping

func Reverse

func Reverse(m Mapping) Mapping

Reverse changes a transformation direction, allowing to construct the source tree.

func Scope

func Scope(name string, m Mapping) Mapping

type MappingOp

type MappingOp interface {
	Op
	Mapping
}

func Bool

func Bool(val bool) MappingOp

Bool asserts that value equals a specific boolean value.

func Int

func Int(val int) MappingOp

Int asserts that value equals a specific integer value.

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.

func String

func String(val string) MappingOp

String asserts that value equals a specific string value.

func Uint

func Uint(val uint) MappingOp

Uint asserts that value equals a specific unsigned integer value.

func Var

func Var(name string) MappingOp

Var stores current node as a value to a named variable in the shared state. Reversal replaces current node with the one from named variable. Variables can store subtrees.

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

type Obj map[string]Op

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) Check

func (o Obj) Check(st *State, n nodes.Node) (bool, error)

Check will convert the operation to Fields and will call Check on it.

func (Obj) CheckObj

func (o Obj) CheckObj(st *State, n nodes.Object) (bool, error)

CheckObj will convert the operation to Fields and will call CheckObj on it.

func (Obj) Construct

func (o Obj) Construct(st *State, n nodes.Node) (nodes.Node, error)

Construct will convert the operation to Fields and will call Construct on it.

func (Obj) ConstructObj

func (o Obj) ConstructObj(st *State, n nodes.Object) (nodes.Object, error)

ConstructObj will convert the operation to Fields and will call ConstructObj on it.

func (Obj) Fields

func (o Obj) Fields() (FieldDescs, bool)

func (Obj) Kinds

func (Obj) Kinds() nodes.Kind

type ObjMap

type ObjMap map[string]Mapping

func (ObjMap) Mapping

func (m ObjMap) Mapping() (src, dst Op)

func (ObjMap) ObjMapping

func (m ObjMap) ObjMapping() (src, dst ObjectOp)

type ObjMapping

type ObjMapping interface {
	Mapping
	ObjMapping() (src, dst ObjectOp)
}

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

type ObjRoles map[string][]role.Role

ObjRoles is a helper type that stores a mapping from field names to their roles.

func (ObjRoles) Mapping

func (o ObjRoles) Mapping() (src, dst Op)

func (ObjRoles) ObjMapping

func (o ObjRoles) ObjMapping() (src, dst ObjectOp)

type ObjectOp

type ObjectOp interface {
	Mod
	ObjectSel

	ConstructObj(st *State, n nodes.Object) (nodes.Object, error)
}

ObjectOp is an operation that is executed on an object. See Object.

func ASTObjectLeft

func ASTObjectLeft(typ string, ast ObjectOp) ObjectOp

ASTObjectLeft construct a native AST shape for a given type name.

func ASTObjectRight

func ASTObjectRight(typ string, norm ObjectOp, rop ArrayOp, roles ...role.Role) ObjectOp

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 CasesObj

func CasesObj(vr string, common ObjectOp, cases ObjectOps) ObjectOp

CasesObj is similar to Cases, but only works on object nodes. It also allows to specify a common set of operations that will be executed for each branch.

It is also required for all branches in CasesObj to have exactly the same set of keys.

Example:

CasesObj("case",
  // common
  Obj{
    "type": String("ident"),
  },
  Objects{
    // case 1: A
    {"name": String("A")},
    // case 1: B
    {"name": String("B")},
  },
)

func CheckObj

func CheckObj(s ObjectSel, op ObjectOp) ObjectOp

CheckObj is similar to Check, but accepts only object operators.

func CommentNode

func CommentNode(block bool, vr string, pos Op) ObjectOp

func JoinObj

func JoinObj(ops ...ObjectOp) ObjectOp

JoinObj will execute all object operations on a specific object in a sequence.

func ObjOpScope

func ObjOpScope(name string, op ObjectOp) ObjectOp

func Operator

func Operator(vr string, lookup map[nodes.Value]ArrayOp, roles ...role.Role) ObjectOp

Operator is a helper to make an AST node describing an operator.

func Part

func Part(vr string, o ObjectOp) ObjectOp

Part defines a partial transformation of an object. All unused fields will be stored into variable with a specified name.

func UASTType

func UASTType(uobj interface{}, op ObjectOp) ObjectOp

func UASTTypePart

func UASTTypePart(vr string, uobj interface{}, op ObjectOp) ObjectOp

type ObjectOps

type ObjectOps interface {
	ObjectOps() []ObjectOp
}

type ObjectSel

type ObjectSel interface {
	Sel
	// Fields returns a map of field names that will be processed by this operation.
	// The flag in the map indicates if the field is required.
	//
	// Returning true as a second argument indicates that the operation will always
	// use all fields. Returning false means that an operation is partial.
	Fields() (FieldDescs, bool)

	CheckObj(st *State, n nodes.Object) (bool, error)
}

ObjectSel is a selector that matches objects. See Object.

func ObjNot

func ObjNot(s ObjectSel) ObjectSel

ObjNot negates all checks on an object, while still asserting this node as an object.

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 Objects

type Objects []ObjectOp

func (Objects) ObjectOps

func (o Objects) ObjectOps() []ObjectOp

type Objs

type Objs []Obj

func (Objs) ObjectOps

func (o Objs) ObjectOps() []ObjectOp

type Op

type Op interface {
	Sel
	Mod
}

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 Any

func Any() Op

Any matches any node and throws it away. It creates a nil node on reversal. See AnyNode for details.

func AnyNode

func AnyNode(create Mod) Op

AnyNode matches any node and throws it away. Reversal will create a node with create op.

This operation should not be used thoughtlessly. Each field that is dropped this way is an information loss and may become a source of bugs.

The preferred way is to assert an exact value with Is or similar operator. If possible, assert the type of expected node or any other field that indicates that this node is useless.

func AnyVal

func AnyVal(val nodes.Value) Op

AnyVal accept any value and creates a provided value on reversal. See AnyNode for details.

func Append

func Append(to Op, items ...ArrayOp) Op

Append is like AppendArr but allows to set more complex first operation. Result of this operation should still be an array.

func ArrWith

func ArrWith(arr Op, items ...Op) Op

func Cases

func Cases(vr string, cases ...Op) Op

Cases acts like a switch statement: it checks multiple operations, picks one that matches node structure and writes the number of the taken branch to the variable.

Operations in branches should not be ambiguous. They should not overlap.

func Check

func Check(s Sel, op Op) Op

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 CommentText(tokens [2]string, vr string) Op

func CommentTextTrimmed

func CommentTextTrimmed(tokens [2]string, vr string) Op

func Each

func Each(vr string, op Op) Op

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

func EachObjectRoles(vr string, roles ...role.Role) Op

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

func EachObjectRolesByType(vr string, types map[string][]role.Role, roles ...role.Role) Op

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 EmptyObj

func EmptyObj() Op

EmptyObj checks that a node is an empty object.

func If

func If(cond string, then, els Op) Op

If checks if a named variable value is true and executes one of sub-operations.

func Lookup

func Lookup(op Op, m map[nodes.Value]nodes.Value) Op

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

func LookupOpVar(vr string, cases map[nodes.Value]Op) Op

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 LookupVar

func LookupVar(vr string, m map[nodes.Value]nodes.Value) Op

LookupVar is a shorthand to lookup value stored in variable.

func NotEmpty

func NotEmpty(op Op) Op

NotEmpty checks that node is not nil and contains one or more fields or elements.

func ObjectRoles

func ObjectRoles(vr string, roles ...role.Role) Op

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

func ObjectRolesCustom(vr string, obj ObjectOp, roles ...role.Role) Op

ObjectRolesCustom is like ObjectRoles but allows to apecify additional conatraints for object.

func ObjectRolesCustomOp

func ObjectRolesCustomOp(vr string, obj ObjectOp, rop ArrayOp, roles ...role.Role) Op

ObjectRolesCustomOp is like ObjectRolesCustom but allows to apecify a custom roles lookup.

func OpScope

func OpScope(name string, op Op) Op

func Opt

func Opt(exists string, op Op) Op

Opt is an optional operation that uses a named variable to store the state.

func OptObjectRoles

func OptObjectRoles(vr string, roles ...role.Role) Op

OptObjectRoles is like ObjectRoles, but marks an object as optional.

func PrependOne

func PrependOne(first Op, arr Op) Op

PrependOne prepends a single element to an array.

func Quote

func Quote(op Op) Op

Quote uses strconv.Quote/Unquote to wrap provided string value.

func SavePosLineCol

func SavePosLineCol(varLine, varCol string) Op

SavePosLineCol makes an operation that describes a uast.Position object with Line and Col field set to named variables.

func SavePosOffset

func SavePosOffset(vr string) Op

SavePosOffset makes an operation that describes a uast.Position object with Offset field set to a named variable.

func Seq

func Seq(ops ...Op) Op

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

func TypedObj(typ string, ops map[string]Op) Op

TypedObj is a shorthand for an object with a specific type and multiples operations on it.

func Uncomment

func Uncomment(vr string, tokens [2]string) Op

Uncomment removes specified tokens from the beginning and from the end of a given string variable.

func UncommentCLike

func UncommentCLike(vr string) Op

UncommentCLike removes // and /* */ symbols from a given string variable.

func ValueConv

func ValueConv(on Op, conv, rev ValueFunc) Op

ValueConv converts a value with a provided function and passes it to sub-operation.

func VarKind

func VarKind(name string, k nodes.Kind) Op

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.

func (ResponseMetadata) Do

func (n ResponseMetadata) Do(root nodes.Node) (nodes.Node, error)

Do applies the transformation described by this object.

type RolesByType

type RolesByType func(typ string) role.Roles

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.

func All

func All(s Sel) Sel

All check matches if all list elements matches sub-check.

func And

func And(sels ...Sel) Sel

And serves as a logical And operation for conditions.

func AnyElem

func AnyElem(s Sel) Sel

AnyElem check matches if any of list elements matches sub-check.

func HasType

func HasType(o interface{}) Sel

func In

func In(vals ...nodes.Value) Sel

In check that the node is a value from a given list.

func Not

func Not(s Sel) Sel

Not negates the check.

func NotNil

func NotNil() Sel

Not nil is a condition that ensures that node is not nil.

func OfKind

func OfKind(k nodes.Kind) Sel

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) ApplyFrom

func (st *State) ApplyFrom(st2 *State)

ApplyFrom merges a provided state into this state object.

func (*State) Clone

func (st *State) Clone() *State

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

func (st *State) GetStateVar(name string) ([]*State, bool)

GetStateVar returns a stored sub-state from a named variable.

func (*State) GetVar

func (st *State) GetVar(name string) (nodes.Node, bool)

GetVar looks up a named variable.

func (*State) MustGetVar

func (st *State) MustGetVar(name string) (nodes.Node, error)

MustGetVar looks up a named variable and returns ErrVariableNotDefined in case it does not exists.

func (*State) MustGetVars

func (st *State) MustGetVars(vars VarsPtrs) error

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

func (st *State) SetStateVar(name string, sub []*State) error

SetStateVar sets a sub-state variable. It returns ErrVariableRedeclared if the variable with this name already exists.

func (*State) SetVar

func (st *State) SetVar(name string, val nodes.Node) error

SetVar sets a named variable. It will return ErrVariableRedeclared if a variable with the same name is already set. It will ignore the operation if variable already exists and has the same value (nodes.Value).

func (*State) SetVars

func (st *State) SetVars(vars Vars) error

SetVars is like SetVar but sets multiple variables in one operation.

func (*State) Validate

func (st *State) Validate() error

Validate should be called after a successful transformation to check if there are any errors related to unused state.

type StringFunc

type StringFunc func(string) (string, error)

StringFunc is a function that transforms string values.

type TransformFunc

type TransformFunc func(n nodes.Node) (nodes.Node, bool, error)

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.

func (TransformFunc) Do

func (f TransformFunc) Do(n nodes.Node) (nodes.Node, error)

Do runs a transformation function for each AST node.

type TransformObjFunc

type TransformObjFunc func(n nodes.Object) (nodes.Object, bool, error)

TransformObjFunc is like TransformFunc, but only matches Object nodes.

func (TransformObjFunc) Do

Do runs a transformation function for each AST node.

func (TransformObjFunc) Func

Func converts this TransformObjFunc to a regular TransformFunc by skipping all non-object nodes.

type Transformer

type Transformer interface {
	Do(root nodes.Node) (nodes.Node, error)
}

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.

type ValueFunc

type ValueFunc func(nodes.Value) (nodes.Value, error)

ValueFunc is a function that transforms values.

type Vars

type Vars map[string]nodes.Node

Vars is a set of variables with their values.

type VarsPtrs

type VarsPtrs map[string]nodes.NodePtr

VarsPtrs is a set of variable pointers.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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