diff --git a/examples/form.go b/examples/form.go index 9c37aaa..a440520 100644 --- a/examples/form.go +++ b/examples/form.go @@ -16,13 +16,13 @@ func main() { flow := dag.NewDAG("Multi-Step Form", "multi-step-form", func(taskID string, result mq.Result) { fmt.Printf("Final result for task %s: %s\n", taskID, string(result.Payload)) }) - flow.AddNode(dag.Page, "FormStep1", "FormStep1", &FormStep1{}) - flow.AddNode(dag.Page, "FormStep2", "FormStep2", &FormStep2{}) - flow.AddNode(dag.Page, "FormResult", "FormResult", &FormResult{}) + flow.AddNode(dag.Page, "Form Step1", "FormStep1", &FormStep1{}) + flow.AddNode(dag.Page, "Form Step2", "FormStep2", &FormStep2{}) + flow.AddNode(dag.Page, "Form Result", "FormResult", &FormResult{}) // Define edges - flow.AddEdge(dag.Simple, "FormStep1", "FormStep1", "FormStep2") - flow.AddEdge(dag.Simple, "FormStep2", "FormStep2", "FormResult") + flow.AddEdge(dag.Simple, "Form Step1", "FormStep1", "FormStep2") + flow.AddEdge(dag.Simple, "Form Step2", "FormStep2", "FormResult") // Start the flow if flow.Error != nil { diff --git a/examples/form.json b/examples/form.json new file mode 100644 index 0000000..ef87dab --- /dev/null +++ b/examples/form.json @@ -0,0 +1,44 @@ +{ + "name": "Multi-Step Form", + "key": "multi-step-form", + "nodes": [ + { + "name": "Form Step1", + "id": "FormStep1", + "node": "Page", + "data": { + "template": "
", + "mapping": {} + }, + "first_node": true + }, + { + "name": "Form Step2", + "id": "FormStep2", + "node": "Page", + "data": { + "template": "", + "mapping": {} + } + }, + { + "name": "Form Result", + "id": "FormResult", + "node": "Page", + "data": { + "template": "Name: {{ name }}
Age: {{ age }}
{{ if register_vote }}You have registered to vote!
{{ else }}You did not register to vote.
{{ end }}", + "mapping": {} + } + } + ], + "edges": [ + { + "source": "FormStep1", + "target": ["FormStep2"] + }, + { + "source": "FormStep2", + "target": ["FormResult"] + } + ] +} diff --git a/examples/json.go b/examples/json.go new file mode 100644 index 0000000..b3f5387 --- /dev/null +++ b/examples/json.go @@ -0,0 +1,158 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "github.com/oarkflow/jet" + "github.com/oarkflow/mq" + "github.com/oarkflow/mq/consts" + "github.com/oarkflow/mq/dag" + "log" +) + +type Data struct { + Template string `json:"template"` + Mapping map[string]string `json:"mapping"` +} + +type Node struct { + Name string `json:"name"` + ID string `json:"id"` + Node string `json:"node"` + Data Data `json:"data"` + FirstNode bool `json:"first_node"` +} + +type Edge struct { + Source string `json:"source"` + Target []string `json:"target"` +} + +type Handler struct { + Name string `json:"name"` + Key string `json:"key"` + Nodes []Node `json:"nodes"` + Edges []Edge `json:"edges"` +} + +func CreateDAGFromJSON(config string) (*dag.DAG, error) { + var handler Handler + err := json.Unmarshal([]byte(config), &handler) + if err != nil { + return nil, fmt.Errorf("failed to parse JSON: %v", err) + } + + flow := dag.NewDAG(handler.Name, handler.Key, func(taskID string, result mq.Result) { + fmt.Printf("Final result for task %s: %s\n", taskID, string(result.Payload)) + }) + + nodeMap := make(map[string]mq.Processor) + + for _, node := range handler.Nodes { + op := &HTMLProcessor{ + Template: node.Data.Template, + Mapping: node.Data.Mapping, + } + nodeMap[node.ID] = op + flow.AddNode(dag.Page, node.Name, node.ID, op, node.FirstNode) + } + + for _, edge := range handler.Edges { + for _, target := range edge.Target { + flow.AddEdge(dag.Simple, edge.Source, edge.Source, target) + } + } + + if flow.Error != nil { + return nil, flow.Error + } + return flow, nil +} + +type HTMLProcessor struct { + dag.Operation + Template string + Mapping map[string]string +} + +func (p *HTMLProcessor) ProcessTask(ctx context.Context, task *mq.Task) mq.Result { + parser := jet.NewWithMemory(jet.WithDelims("{{", "}}")) + data := map[string]interface{}{ + "task_id": ctx.Value("task_id"), + } + + for key, value := range p.Mapping { + data[key] = value + } + + rs, err := parser.ParseTemplate(p.Template, data) + if err != nil { + return mq.Result{Error: err, Ctx: ctx} + } + + ctx = context.WithValue(ctx, consts.ContentType, consts.TypeHtml) + response := map[string]interface{}{ + "html_content": rs, + } + payload, _ := json.Marshal(response) + + return mq.Result{Payload: payload, Ctx: ctx} +} + +func main() { + // JSON configuration + jsonConfig := `{ + "name": "Multi-Step Form", + "key": "multi-step-form", + "nodes": [ + { + "name": "Form Step1", + "id": "FormStep1", + "node": "Page", + "data": { + "template": "\n\n\n\n", + "mapping": {} + }, + "first_node": true + }, + { + "name": "Form Step2", + "id": "FormStep2", + "node": "Page", + "data": { + "template": "\n\n\n\n", + "mapping": { + "show_voting_controls": "conditional" + } + } + }, + { + "name": "Form Result", + "id": "FormResult", + "node": "Page", + "data": { + "template": "\n\nName: {{ name }}
\nAge: {{ age }}
\n{{ if register_vote }}\nYou have registered to vote!
\n{{ else }}\nYou did not register to vote.
\n{{ end }}\n\n", + "mapping": {} + } + } + ], + "edges": [ + { + "source": "FormStep1", + "target": ["FormStep2"] + }, + { + "source": "FormStep2", + "target": ["FormResult"] + } + ] +}` + + flow, err := CreateDAGFromJSON(jsonConfig) + if err != nil { + log.Fatalf("Error creating DAG: %v", err) + } + + flow.Start(context.Background(), "0.0.0.0:8082") +}