93 lines
2.3 KiB
Go
93 lines
2.3 KiB
Go
package apiconfig
|
|
|
|
import "git.servflow.io/servflow/definitions/proto"
|
|
|
|
func ProtoConfigToAPIConfig(configs []*proto.WorkflowConfig) []*APIConfig {
|
|
cfgs := make([]*APIConfig, 0)
|
|
for i := range configs {
|
|
protoConfig := configs[i]
|
|
if protoConfig == nil || protoConfig.Id == "" || protoConfig.Status == proto.Status_INACTIVE {
|
|
continue
|
|
}
|
|
a := APIConfig{
|
|
ID: protoConfig.Id,
|
|
ListenPath: protoConfig.ListenPath,
|
|
Method: protoConfig.Method,
|
|
Request: RequestConfig{
|
|
Next: protoConfig.Request.Next,
|
|
},
|
|
Actions: map[string]Action{},
|
|
Conditionals: map[string]Conditional{},
|
|
Responses: map[string]ResponseConfig{},
|
|
McpTool: MCPToolConfig{
|
|
Enabled: protoConfig.Mcp.Enabled,
|
|
Name: protoConfig.Mcp.Name,
|
|
Result: protoConfig.Mcp.Result,
|
|
Description: protoConfig.Mcp.Description,
|
|
Start: protoConfig.Mcp.Start,
|
|
Args: make(map[string]ArgType),
|
|
},
|
|
HttpConfig: HttpConfig{
|
|
Next: protoConfig.Request.Next,
|
|
ListenPath: protoConfig.Request.ListenPath,
|
|
Method: protoConfig.Request.Method,
|
|
},
|
|
}
|
|
|
|
for k := range protoConfig.Mcp.Args {
|
|
a.McpTool.Args[k] = ArgType{
|
|
Name: protoConfig.Mcp.Args[k].Name,
|
|
Type: protoConfig.Mcp.Args[k].Type,
|
|
}
|
|
}
|
|
|
|
for key := range protoConfig.Actions {
|
|
act := protoConfig.Actions[key]
|
|
a.Actions[key] = Action{
|
|
Type: act.Type,
|
|
Config: act.Config,
|
|
Next: act.Next,
|
|
Fail: act.Fail,
|
|
}
|
|
}
|
|
|
|
for key := range protoConfig.Conditionals {
|
|
cond := protoConfig.Conditionals[key]
|
|
a.Conditionals[key] = Conditional{
|
|
ValidPath: cond.ValidPath,
|
|
InvalidPath: cond.InvalidPath,
|
|
Expression: cond.Expression,
|
|
}
|
|
}
|
|
|
|
for key := range protoConfig.Responses {
|
|
resp := protoConfig.Responses[key]
|
|
a.Responses[key] = ResponseConfig{
|
|
Code: int(resp.GetCode()),
|
|
Type: resp.Type,
|
|
Template: string(resp.Template),
|
|
}
|
|
}
|
|
|
|
a.Normalize()
|
|
cfgs = append(cfgs, &a)
|
|
}
|
|
|
|
return cfgs
|
|
}
|
|
|
|
func (a *APIConfig) Normalize() {
|
|
if a.HttpConfig.Method == "" {
|
|
a.HttpConfig.Method = a.Method
|
|
}
|
|
if a.HttpConfig.Next == "" {
|
|
a.HttpConfig.Next = a.Request.Next
|
|
}
|
|
if a.HttpConfig.ListenPath == "" {
|
|
a.HttpConfig.ListenPath = a.ListenPath
|
|
}
|
|
if len(a.HttpConfig.CORSAllowedOrigins) < 1 {
|
|
a.HttpConfig.CORSAllowedOrigins = a.Request.CORSAllowedOrigins
|
|
}
|
|
}
|