Documentation
¶
Index ¶
- type Pipeline
- func (pipeline Pipeline) Execute(inSchema schema.Schema, inObject any, outSchema schema.Schema, outObject any) error
- func (pipeline Pipeline) IsEmpty() bool
- func (pipeline Pipeline) MarshalJSON() ([]byte, error)
- func (pipeline Pipeline) MarshalSliceOfMap() []map[string]any
- func (pipeline Pipeline) NotEmpty() bool
- type Rule
- func Append(append any, target string) Rule
- func Condition(condition string, thenRules []Rule, elseRules []Rule) Rule
- func Expression(expression string, target string) Rule
- func ForEach(sourcePath string, targetPath string, filter string, rulesMap []map[string]any) Rule
- func Path(from string, target string) Rule
- func Value(value any, target string) Rule
- type Runner
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Pipeline ¶
type Pipeline []Rule
Pipeline represents a slice of Rule objects
func New ¶
New returns a new Pipeline object, populated with the provided rules
Example ¶
// Define rules for the translation Pipeline
rules := []Rule{
Expression("{{.firstName}} {{.lastName}}", "fullName"),
Path("email", "email"),
Value("person", "type"),
Condition(`{{eq "M" .gender}}`, []Rule{
Expression("{{.firstName}} is Male", "comment"),
}, []Rule{
Expression("{{.firstName}} is not Malr", "comment"),
}),
}
// Add all of the rules to a new Pipeline
translator := New(rules...)
fmt.Println(translator)
func NewFromJSON ¶
NewFromJSON reads a JSON string and returns a Pipeline object
Example ¶
// Import JSON from external source
rulesJSON := `[
{"expression": "{{.firstName}} {{.lastName}}", "target": "fullName"},
{"path": "email", "target": "email"},
{"value": "person", "target": "type"},
{"if": "{{eq \"M\" .gender}}", "then": [
{"target": "comment", "expression": "{{.firstName}} is Male"}
], "else": [
{"target": "comment", "expression": "{{.firstName}} is not Male"}
]}
]`
// Unmarshal JSON directly into a Pipeline
rules, _ := NewFromMap()
if json.Unmarshal([]byte(rulesJSON), &rules) != nil {
fmt.Println("Error parsing JSON")
}
// Success!
fmt.Println(rules)
func NewFromMap ¶
NewFromMap parses a slice of mapof.Any objects into a Pipeline
Example ¶
// Define rules as a mapof.Any
rules := []map[string]any{
{"expression": "{{.firstName}} {{.lastName}}", "target": "fullName"},
{"path": "email", "target": "email"},
{"value": "person", "target": "type"},
{"if": `{{eq "M" .gender}}`, "then": []mapof.Any{
{"target": "comment", "expression": "{{.firstName}} is Male"},
}, "else": []mapof.Any{
{"target": "comment", "expression": "{{.firstName}} is not Male"},
}},
}
// Create a new Pipeline from the rules
if translator, err := NewFromMap(rules...); err != nil {
fmt.Println(err)
} else {
fmt.Println(translator)
}
func NewSliceOfPipelines ¶ added in v0.23.2
NewSliceOfPipelines parses a slice of maps into a slice of Pipelines. If any of the maps DOES NOT represent a valid Pipeline, then the function will return an error.
func (Pipeline) Execute ¶
func (pipeline Pipeline) Execute(inSchema schema.Schema, inObject any, outSchema schema.Schema, outObject any) error
Execute runs each of the rules in the Pipeline
Example ¶
// SOURCE DATA
sourceValue := mapof.Any{
"firstName": "John",
"lastName": "Connor",
"email": "john@connor.mil",
"gender": "M",
}
// TARGET CONFIGURATION
targetSchema := schema.New(schema.Object{
Properties: schema.ElementMap{
"fullName": schema.String{},
"email": schema.String{Format: "email"},
"type": schema.String{},
"comment": schema.String{},
},
})
targetValue := mapof.Any{}
// CREATE MAPPING RULES
rules, err := NewFromMap(
mapof.Any{"target": "fullName", "expression": "{{.firstName}} {{.lastName}}"},
mapof.Any{"target": "email", "path": "email"},
mapof.Any{"target": "type", "value": "person"},
mapof.Any{"if": "{{eq \"M\" .gender}}", "then": []mapof.Any{
{"target": "comment", "expression": "{{.firstName}} is Male"},
}, "else": []mapof.Any{
{"target": "comment", "expression": "{{.firstName}} is not Male"},
}},
)
derp.Report(err)
// MAP DATA FROM SOURCE TO TARGET
err = rules.Execute(schema.Wildcard(), sourceValue, targetSchema, &targetValue)
derp.Report(err)
// OUTPUT RESULTS
fmt.Println(targetValue.GetString("fullName"))
fmt.Println(targetValue.GetString("email"))
fmt.Println(targetValue.GetString("type"))
fmt.Println(targetValue.GetString("comment"))
Output: John Connor john@connor.mil person John is Male
func (Pipeline) MarshalJSON ¶ added in v0.23.2
func (Pipeline) MarshalSliceOfMap ¶ added in v0.23.2
type Rule ¶
type Rule struct {
Runner
}
Rule represents a single mapping rule
func Append ¶ added in v0.24.0
Append creates a new Rule that writes a constant value to the output object
func Condition ¶
Condition creates a new Rule that executes a condition, and then runs a set of rules based on the result
func Expression ¶
Expression creates a new Rule that executes a template expression
func (*Rule) MarshalJSON ¶ added in v0.23.2
func (*Rule) MarshalMap ¶ added in v0.23.2
func (*Rule) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaller interface
type Runner ¶
type Runner interface {
// Execute runs the rule on the input object, and writes the result to the output object
Execute(inSchema schema.Schema, inObject any, outSchema schema.Schema, outObject any) error
// MarshalMap converts the object to a mapof.Any
MarshalMap() map[string]any
}
Runner in the interface for objects that implement Rules