Skip to content

Commit

Permalink
ADD common template process method for flow action.
Browse files Browse the repository at this point in the history
ADD common template process method for flow action.
  • Loading branch information
karminski committed Jan 26, 2024
1 parent 6dd0d1e commit 016aacf
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/actionruntime/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package common
import (
"database/sql"
"database/sql/driver"
"encoding/json"

parser_template "github.com/illacloud/builder-backend/src/utils/parser/template"
)

func RetrieveToMap(rows *sql.Rows) ([]map[string]interface{}, error) {
Expand Down Expand Up @@ -98,3 +101,74 @@ func RetrieveToMapByDriverRows(rows driver.Rows) ([]map[string]interface{}, erro

return mapData, nil
}

func ProcessTemplateByContext(template interface{}, context map[string]interface{}) (interface{}, error) {
processorMethod := func(template interface{}, context map[string]interface{}) (interface{}, error) {
valueInMap, valueIsMap := template.(map[string]interface{})
if valueIsMap {
processedValue, errInPreprocessTemplate := ProcessTemplateByContext(valueInMap, context)
if errInPreprocessTemplate != nil {
return nil, errInPreprocessTemplate
}
return processedValue, nil
}

// check if value is string, then process it
valueInString, valueIsString := template.(string)
if valueIsString {
// check if value is json string
var valueInJson interface{}
errInUnmarshal := json.Unmarshal([]byte(valueInString), &valueInJson)
itIsJSONString := errInUnmarshal == nil
if itIsJSONString {
// json string, process it as array or map
processedValue, errInPreprocessTemplate := ProcessTemplateByContext(valueInJson, context)
if errInPreprocessTemplate != nil {
return nil, errInPreprocessTemplate
}
return processedValue, nil
} else {
// jsut a normal string
processedTemplate, errInAssembleTemplate := parser_template.AssembleTemplateWithVariable(valueInString, context)
if errInAssembleTemplate != nil {
return nil, errInAssembleTemplate
}
return processedTemplate, nil
}
}
return template, nil
}

// assert input
inputInSLice, inputIsSlice := template.([]interface{})
inputInMap, inputIsMap := template.(map[string]interface{})
inputInString, inputIsString := template.(string)

// process it
if inputIsSlice {
newSlice := make([]interface{}, 0)
for _, value := range inputInSLice {
processedTemplate, errInProcess := processorMethod(value, context)
if errInProcess != nil {
return nil, errInProcess
}
newSlice = append(newSlice, processedTemplate)
}
return newSlice, nil
}
if inputIsMap {
newMap := make(map[string]interface{}, 0)
for key, value := range inputInMap {
processedTemplate, errInProcess := processorMethod(value, context)
if errInProcess != nil {
return nil, errInProcess
}
newMap[key] = processedTemplate
}
return newMap, nil
}
if inputIsString {
return processorMethod(inputInString, context)
}
return template, nil
}
9 changes: 9 additions & 0 deletions src/controller/flow_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/gin-gonic/gin"
"github.com/illacloud/builder-backend/src/actionruntime/common"
"github.com/illacloud/builder-backend/src/model"
"github.com/illacloud/builder-backend/src/request"
"github.com/illacloud/builder-backend/src/response"
Expand Down Expand Up @@ -345,6 +346,14 @@ func (controller *Controller) RunFlowAction(c *gin.Context) {
flowAction.UpdateWithRunFlowActionRequest(runFlowActionRequest, userID)
fmt.Printf("[DUMP] flowAction: %+v\n", flowAction)

// process input context with action template
processedTemplate, errInProcessTemplate := common.ProcessTemplateByContext(flowAction.ExportTemplateInMap(), runFlowActionRequest.ExportContext())
if errInProcessTemplate != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_PROCESS_FLOW_ACTION, "process flow action failed: "+errInProcessTemplate.Error())
return
}
flowAction.SetTemplate(processedTemplate)

// assembly flowAction
flowActionFactory := model.NewFlowActionFactoryByFlowAction(flowAction)
flowActionAssemblyLine, errInBuild := flowActionFactory.Build()
Expand Down
1 change: 1 addition & 0 deletions src/controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ const (
ERROR_FLAG_CAN_NOT_DELETE_FLOW_ACTION = "ERROR_FLAG_CAN_NOT_DELETE_FLOW_ACTION"
ERROR_FLAG_EXECUTE_FLOW_ACTION_FAILED = "ERROR_FLAG_EXECUTE_FLOW_ACTION_FAILED"
ERROR_FLAG_CAN_NOT_PARSE_EXPIRE_AT_TIME = "ERROR_FLAG_CAN_NOT_PARSE_EXPIRE_AT_TIME"
ERROR_FLAG_CAN_NOT_PROCESS_FLOW_ACTION = "ERROR_FLAG_CAN_NOT_PROCESS_FLOW_ACTION"
)

var SKIPPING_MAGIC_ID = map[string]int{
Expand Down
5 changes: 5 additions & 0 deletions src/model/flow_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func (action *FlowAction) InitForFork(teamID int, workflowID int, version int, u
action.InitUpdatedAt()
}

func (action *FlowAction) SetTemplate(tempalte interface{}) {
templateInJSONByte, _ := json.Marshal(tempalte)
action.Template = string(templateInJSONByte)
}

func (action *FlowAction) AppendNewVersion(newVersion int) {
action.CleanID()
action.InitUID()
Expand Down

0 comments on commit 016aacf

Please sign in to comment.