Documentation ¶
Index ¶
- type Pipeline
- type 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)
Output:
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)
Output:
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) }
Output:
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{"expression": "{{.firstName}} {{.lastName}}", "target": "fullName"}, mapof.Any{"path": "email", "target": "email"}, mapof.Any{"value": "person", "target": "type"}, 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
type Rule ¶
type Rule struct {
Runner
}
Rule represents a single mapping rule
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) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaller interface
Click to show internal directories.
Click to hide internal directories.