sdk: Index | Files | Directories

package transformer

import ""


Package Files

arrays.go ast.go conv.go errors.go legacy.go ops.go semantic.go transformer.go


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

func NewErrUnusedField Uses

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 Uses

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 Uses

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.

type ArrayOp Uses

type ArrayOp interface {
    // 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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

func One(op Op) ArrayOp

One is a shorthand for a list with one element.

func Roles Uses

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

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

type ByFieldName Uses

type ByFieldName []Field

ByFieldName will sort fields descriptions by their names.

func (ByFieldName) Len Uses

func (arr ByFieldName) Len() int

func (ByFieldName) Less Uses

func (arr ByFieldName) Less(i, j int) bool

func (ByFieldName) Swap Uses

func (arr ByFieldName) Swap(i, j int)

type CodeTransformer Uses

type CodeTransformer interface {
    OnCode(code string) Transformer

CodeTransformer is a special case of Transformer that needs an original source code to operate.

type Field Uses

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 Uses

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 Uses

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 Uses

func (f Field) Desc() FieldDesc

Desc returns a field descriptor.

type FieldDesc Uses

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 Uses

func (f *FieldDesc) SetValue(sel Sel)

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

type FieldDescs Uses

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 Uses

func NewFieldDescs(n int) FieldDescs

func (*FieldDescs) CheckObj Uses

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 Uses

func (f *FieldDescs) Clone() FieldDescs

Clone makes a copy of field description, without cloning each field values.

func (*FieldDescs) Get Uses

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

Get the field descriptor by its name.

func (*FieldDescs) Has Uses

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

Has checks if a field with a given name exists.

func (*FieldDescs) Index Uses

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

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

func (*FieldDescs) Len Uses

func (f *FieldDescs) Len() int

Len returns a number of fields.

func (*FieldDescs) Set Uses

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

Set a field descriptor by name.

type FieldRole Uses

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 Uses

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 Uses

func (f FieldRoles) Mapping() (src, dst Op)

func (FieldRoles) ObjMapping Uses

func (f FieldRoles) ObjMapping() (src, dst ObjectOp)

type Fields Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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

func (Fields) Kinds Uses

func (Fields) Kinds() nodes.Kind

type Has Uses

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 Uses

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 Uses

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 Uses

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

func (Has) Kinds Uses

func (Has) Kinds() nodes.Kind

type HasFields Uses

type HasFields map[string]bool

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

func (HasFields) Check Uses

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 Uses

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 Uses

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

func (HasFields) Kinds Uses

func (HasFields) Kinds() nodes.Kind

type Mapping Uses

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

func AnnotateIfNoRoles Uses

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 Uses

func Identity(op Op) Mapping

func Map Uses

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 Uses

func MapEach(vr string, m Mapping) Mapping

func Reverse Uses

func Reverse(m Mapping) Mapping

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

func Scope Uses

func Scope(name string, m Mapping) Mapping

type MappingOp Uses

type MappingOp interface {

func Bool Uses

func Bool(val bool) MappingOp

Bool asserts that value equals a specific boolean value.

func Int Uses

func Int(val int) MappingOp

Int asserts that value equals a specific integer value.

func Is Uses

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 Uses

func String(val string) MappingOp

String asserts that value equals a specific string value.

func Uint Uses

func Uint(val uint) MappingOp

Uint asserts that value equals a specific unsigned integer value.

func Var Uses

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 Uses

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 Uses

type MultiError struct {
    Errs []error

MultiError is an error that groups multiple other errors.

func (*MultiError) Error Uses

func (e *MultiError) Error() string

type Obj Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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

func (Obj) Kinds Uses

func (Obj) Kinds() nodes.Kind

type ObjMap Uses

type ObjMap map[string]Mapping

func (ObjMap) Mapping Uses

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

func (ObjMap) ObjMapping Uses

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

type ObjMapping Uses

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

func AnnotateType Uses

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 Uses

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 Uses

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 Uses

func MapObj(src, dst ObjectOp) ObjMapping

func MapPart Uses

func MapPart(vr string, m ObjMapping) ObjMapping

func MapSemantic Uses

func MapSemantic(nativeType string, semType interface{}, m ObjMapping) ObjMapping

func MapSemanticPos Uses

func MapSemanticPos(nativeType string, semType interface{}, pos map[string]string, m ObjMapping) ObjMapping

func ObjScope Uses

func ObjScope(name string, m ObjMapping) ObjMapping

type ObjRoles Uses

type ObjRoles map[string][]role.Role

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

func (ObjRoles) Mapping Uses

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

func (ObjRoles) ObjMapping Uses

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

type ObjectOp Uses

type ObjectOp interface {

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

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

func ASTObjectLeft Uses

func ASTObjectLeft(typ string, ast ObjectOp) ObjectOp

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

func ASTObjectRight Uses

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

ASTObjectRight constructs an annotated native AST shape with specific roles.

func ASTObjectRightCustom Uses

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 Uses

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.


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

func CheckObj Uses

func CheckObj(s ObjectSel, op ObjectOp) ObjectOp

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

func CommentNode Uses

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

func JoinObj Uses

func JoinObj(ops ...ObjectOp) ObjectOp

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

func ObjOpScope Uses

func ObjOpScope(name string, op ObjectOp) ObjectOp

func Operator Uses

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 Uses

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 Uses

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

func UASTTypePart Uses

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

type ObjectOps Uses

type ObjectOps interface {
    ObjectOps() []ObjectOp

type ObjectSel Uses

type ObjectSel interface {
    // 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 Uses

func ObjNot(s ObjectSel) ObjectSel

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

type ObjectToNode Uses

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 Uses

func (n ObjectToNode) Mapping() Mapping

Mapping construct a transformation from ObjectToNode definition.

type Objects Uses

type Objects []ObjectOp

func (Objects) ObjectOps Uses

func (o Objects) ObjectOps() []ObjectOp

type Objs Uses

type Objs []Obj

func (Objs) ObjectOps Uses

func (o Objs) ObjectOps() []ObjectOp

type Op Uses

type Op interface {

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 Uses

func Any() Op

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

func AnyNode Uses

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 Uses

func AnyVal(val nodes.Value) Op

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

func Append Uses

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 Uses

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

func Cases Uses

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 Uses

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 Uses

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

func CommentTextTrimmed Uses

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

func Each Uses

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 Uses

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 Uses

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 Uses

func EmptyObj() Op

EmptyObj checks that a node is an empty object.

func If Uses

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 Uses

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 Uses

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 Uses

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

LookupVar is a shorthand to lookup value stored in variable.

func NotEmpty Uses

func NotEmpty(op Op) Op

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

func ObjectRoles Uses

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 Uses

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

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

func ObjectRolesCustomOp Uses

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 Uses

func OpScope(name string, op Op) Op

func Opt Uses

func Opt(exists string, op Op) Op

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

func OptObjectRoles Uses

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

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

func PrependOne Uses

func PrependOne(first Op, arr Op) Op

PrependOne prepends a single element to an array.

func Quote Uses

func Quote(op Op) Op

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

func SavePosLineCol Uses

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 Uses

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 Uses

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 Uses

func StringConv(on Op, conv, rev StringFunc) Op

StringConv is like ValueConv, but only processes string arguments.

func TypedObj Uses

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 Uses

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 Uses

func UncommentCLike(vr string) Op

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

func ValueConv Uses

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

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

func VarKind Uses

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

type ResponseMetadata Uses

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 Uses

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

Do applies the transformation described by this object.

type RolesByType Uses

type RolesByType func(typ string) role.Roles

RolesByType is a function for getting roles for specific AST node type.

type Sel Uses

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 Uses

func All(s Sel) Sel

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

func And Uses

func And(sels ...Sel) Sel

And serves as a logical And operation for conditions.

func AnyElem Uses

func AnyElem(s Sel) Sel

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

func HasType Uses

func HasType(o interface{}) Sel

func In Uses

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

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

func Not Uses

func Not(s Sel) Sel

Not negates the check.

func NotNil Uses

func NotNil() Sel

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

func OfKind Uses

func OfKind(k nodes.Kind) Sel

type State Uses

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 Uses

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 Uses

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

ApplyFrom merges a provided state into this state object.

func (*State) Clone Uses

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 Uses

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

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

func (*State) GetVar Uses

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

GetVar looks up a named variable.

func (*State) MustGetVar Uses

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 Uses

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

MustGetVars is like MustGetVar but fetches multiple variables in one operation.

func (*State) Reset Uses

func (st *State) Reset()

Reset clears the state and allows to reuse an object.

func (*State) SetStateVar Uses

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 Uses

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 Uses

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

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

func (*State) Validate Uses

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 Uses

type StringFunc func(string) (string, error)

StringFunc is a function that transforms string values.

type TransformFunc Uses

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 Uses

func RolesDedup() TransformFunc

RolesDedup is an irreversible transformation that removes duplicate roles from AST nodes.

func (TransformFunc) Do Uses

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

Do runs a transformation function for each AST node.

type TransformObjFunc Uses

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

TransformObjFunc is like TransformFunc, but only matches Object nodes.

func (TransformObjFunc) Do Uses

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

Do runs a transformation function for each AST node.

func (TransformObjFunc) Func Uses

func (f TransformObjFunc) Func() TransformFunc

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

type Transformer Uses

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 Uses

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 Uses

func Mappings(maps ...Mapping) Transformer

Mappings takes multiple mappings and optimizes the process of applying them as a single transformation.

func Transformers Uses

func Transformers(arr ...[]Transformer) []Transformer

Transformers appends all provided transformer slices into single one.

type ValueFunc Uses

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

ValueFunc is a function that transforms values.

type Vars Uses

type Vars map[string]nodes.Node

Vars is a set of variables with their values.

type VarsPtrs Uses

type VarsPtrs map[string]nodes.NodePtr

VarsPtrs is a set of variable pointers.



Package transformer imports 10 packages (graph). Updated 2019-11-01. Refresh now. Tools for package owners.