Skip to content

Commit

Permalink
Add number and boolean support
Browse files Browse the repository at this point in the history
  • Loading branch information
canack committed Jan 4, 2024
1 parent 7bdfa02 commit bb5563e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 9 deletions.
39 changes: 37 additions & 2 deletions internal/terminal/handler/ghtrigger/ghtrigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}
}
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down
48 changes: 47 additions & 1 deletion pkg/workflow/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package workflow
import (
"encoding/json"
"reflect"
"strconv"

py "github.com/termkit/gama/pkg/yaml"
)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -213,6 +258,7 @@ func convertJsonToString(m map[string]interface{}) error {
type Pretty struct {
Choices []PrettyChoice
Inputs []PrettyInput
Boolean []PrettyInput
KeyVals []PrettyKeyValue
}

Expand Down
14 changes: 12 additions & 2 deletions pkg/workflow/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ on:
"event-logger-ref": "main",
"network-api-ref": "main",
"analytics-service-ref": "main"
}'
}'
deployment_zone:
description: 'Deployment Zone'
type: choice
Expand All @@ -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
`)

Expand Down
15 changes: 12 additions & 3 deletions pkg/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
12 changes: 11 additions & 1 deletion pkg/yaml/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ on:
"event-logger-ref": "main",
"network-api-ref": "main",
"analytics-service-ref": "main"
}'
}'
deployment_zone:
description: 'Deployment Zone'
type: choice
Expand All @@ -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
`)

Expand Down

0 comments on commit bb5563e

Please sign in to comment.