From bb5563e8f8e0fc082c0960a0590b2dec58f4ecad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Engin=20A=C3=A7=C4=B1kg=C3=B6z?= Date: Fri, 5 Jan 2024 02:49:47 +0300 Subject: [PATCH] Add number and boolean support --- .../terminal/handler/ghtrigger/ghtrigger.go | 39 ++++++++++++++- pkg/workflow/workflow.go | 48 ++++++++++++++++++- pkg/workflow/workflow_test.go | 14 +++++- pkg/yaml/yaml.go | 15 ++++-- pkg/yaml/yaml_test.go | 12 ++++- 5 files changed, 119 insertions(+), 9 deletions(-) diff --git a/internal/terminal/handler/ghtrigger/ghtrigger.go b/internal/terminal/handler/ghtrigger/ghtrigger.go index 2bee860..64fd6d7 100644 --- a/internal/terminal/handler/ghtrigger/ghtrigger.go +++ b/internal/terminal/handler/ghtrigger/ghtrigger.go @@ -184,7 +184,9 @@ func (m *ModelGithubTrigger) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } func (m *ModelGithubTrigger) switchBetweenInputAndTable() { - if m.tableTrigger.SelectedRow()[1] == "input" { + var selectedRow = m.tableTrigger.SelectedRow() + + if selectedRow[1] == "input" || selectedRow[1] == "bool" { m.textInput.Focus() m.tableTrigger.Blur() } else { @@ -287,6 +289,22 @@ func (m *ModelGithubTrigger) inputController(ctx context.Context) { m.tableTrigger.SetRows(rows) } } + + for i, boolean := range m.workflowContent.Boolean { + if fmt.Sprintf("%d", boolean.ID) == selectedRow[0] { + m.textInput.Placeholder = boolean.Default + m.workflowContent.Boolean[i].SetValue(m.textInput.Value()) + + rows := m.tableTrigger.Rows() + for i, row := range rows { + if row[0] == selectedRow[0] { + rows[i][4] = m.textInput.Value() + } + } + + m.tableTrigger.SetRows(rows) + } + } } } } @@ -315,9 +333,10 @@ func (m *ModelGithubTrigger) View() string { doc := strings.Builder{} doc.WriteString(baseStyle.Render(m.tableTrigger.View())) + var selectedRow = m.tableTrigger.SelectedRow() var selector = m.emptySelector() if len(m.tableTrigger.Rows()) > 0 { - if m.tableTrigger.SelectedRow()[1] == "input" { + if selectedRow[1] == "input" || selectedRow[1] == "bool" { selector = m.inputSelector() } else { selector = m.optionSelector() @@ -397,6 +416,16 @@ func (m *ModelGithubTrigger) syncWorkflowContent(ctx context.Context) { }) } + for _, boolean := range m.workflowContent.Boolean { + tableRowsTrigger = append(tableRowsTrigger, table.Row{ + fmt.Sprintf("%d", boolean.ID), + "bool", + boolean.Key, + boolean.Default, + boolean.Value, + }) + } + m.tableTrigger.SetRows(tableRowsTrigger) m.tableTrigger.SetCursor(0) @@ -476,6 +505,12 @@ func (m *ModelGithubTrigger) fillEmptyValuesWithDefault() { m.workflowContent.KeyVals[i].SetValue(keyVal.Default) } } + + for i, boolean := range m.workflowContent.Boolean { + if boolean.Value == "" { + m.workflowContent.Boolean[i].SetValue(boolean.Default) + } + } } func (m *ModelGithubTrigger) triggerWorkflow() { diff --git a/pkg/workflow/workflow.go b/pkg/workflow/workflow.go index b45fcde..38bd9cd 100644 --- a/pkg/workflow/workflow.go +++ b/pkg/workflow/workflow.go @@ -3,6 +3,7 @@ package workflow import ( "encoding/json" "reflect" + "strconv" py "github.com/termkit/gama/pkg/yaml" ) @@ -25,6 +26,9 @@ type Content struct { // Value is a map of string and value designed for string Value *Value + + // Boolean is a map of string and value designed for boolean + Boolean *Value } type KeyValue struct { @@ -87,7 +91,7 @@ func ParseWorkflow(content py.WorkflowContent) (*Workflow, error) { } } - if value.Type == "string" || value.Type == "boolean" || value.Type == "number" || value.Type == "" { + if value.Type == "string" || value.Type == "number" || value.Type == "" { defaultValue := "" if value.Default != nil { _, ok := value.Default.(string) @@ -105,6 +109,26 @@ func ParseWorkflow(content py.WorkflowContent) (*Workflow, error) { }, } } + + if value.Type == "boolean" { + defaultValue := "false" + if value.Default != nil { + _, ok := value.Default.(bool) + if ok { + strBool := strconv.FormatBool(value.Default.(bool)) + defaultValue = strBool + } + } + w.Content[key] = Content{ + Description: value.Description, + Type: "boolean", + Required: value.Required, + Boolean: &Value{ + Default: defaultValue, + Value: "", + }, + } + } } return w, nil @@ -152,6 +176,22 @@ func (w *Workflow) ToPretty() *Pretty { }) id++ } + if data.Boolean != nil { + var defaultValue string + if data.Boolean.Default != nil { + _, ok := data.Boolean.Default.(string) + if ok { + defaultValue = data.Boolean.Default.(string) + } + } + pretty.Boolean = append(pretty.Boolean, PrettyInput{ + ID: id, + Key: parent, + Value: "", + Default: defaultValue, + }) + id++ + } } return &pretty @@ -184,6 +224,11 @@ func (p *Pretty) ToJson() (string, error) { result[i.Key] = i.Value } + // Process Boolean + for _, b := range p.Boolean { + result[b.Key] = b.Value + } + if err := convertJsonToString(result); err != nil { return "", err } @@ -213,6 +258,7 @@ func convertJsonToString(m map[string]interface{}) error { type Pretty struct { Choices []PrettyChoice Inputs []PrettyInput + Boolean []PrettyInput KeyVals []PrettyKeyValue } diff --git a/pkg/workflow/workflow_test.go b/pkg/workflow/workflow_test.go index 65d28a8..2bff8ac 100644 --- a/pkg/workflow/workflow_test.go +++ b/pkg/workflow/workflow_test.go @@ -24,7 +24,7 @@ on: "event-logger-ref": "main", "network-api-ref": "main", "analytics-service-ref": "main" - }' + }' deployment_zone: description: 'Deployment Zone' type: choice @@ -35,13 +35,23 @@ on: - 'gamma' - 'delta' - 'epsilon' - - 'trial' + - 'zeta' default: 'trial' industry_category: description: 'Industry Category' type: string required: true default: 'general' + boolean_flag: + description: 'Boolean Flag' + type: boolean + required: true + default: true + number: + description: 'Number' + type: number + required: true + default: 1 secrets: inherit `) diff --git a/pkg/yaml/yaml.go b/pkg/yaml/yaml.go index 0991d60..71a3bf9 100644 --- a/pkg/yaml/yaml.go +++ b/pkg/yaml/yaml.go @@ -31,13 +31,22 @@ func (i *WorkflowInput) UnmarshalYAML(unmarshal func(interface{}) error) error { return err } - // Attempt to unmarshal JSON content if the default value is a string - if defaultStr, ok := i.Default.(string); ok { + // Process the default value based on its actual type + switch def := i.Default.(type) { + case string: + // Attempt to unmarshal JSON content if the default value is a string tempMap := make(map[string]string) - if err := json.Unmarshal([]byte(defaultStr), &tempMap); err == nil { + if err := json.Unmarshal([]byte(def), &tempMap); err == nil { i.JSONContent = tempMap } + case bool: + // Handle boolean values + i.Default = def + case float64: + // Handle number values (YAML unmarshals numbers to float64 by default) + i.Default = def } + return nil } diff --git a/pkg/yaml/yaml_test.go b/pkg/yaml/yaml_test.go index e40ce8c..f969bc7 100644 --- a/pkg/yaml/yaml_test.go +++ b/pkg/yaml/yaml_test.go @@ -23,7 +23,7 @@ on: "event-logger-ref": "main", "network-api-ref": "main", "analytics-service-ref": "main" - }' + }' deployment_zone: description: 'Deployment Zone' type: choice @@ -41,6 +41,16 @@ on: type: string required: true default: 'general' + boolean_flag: + description: 'Boolean Flag' + type: boolean + required: true + default: true + number: + description: 'Number' + type: number + required: true + default: 1 secrets: inherit `)