Skip to content

Commit 91b1a35

Browse files
authored
Consolidate duplicate string extraction functions in pkg/workflow (#3626)
1 parent 6d0b1df commit 91b1a35

4 files changed

Lines changed: 13 additions & 24 deletions

File tree

pkg/workflow/compiler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ func (c *Compiler) ParseWorkflowFile(markdownPath string) (*WorkflowData, error)
915915
}
916916

917917
// Check if frontmatter specifies a custom name and use it instead
918-
frontmatterName := extractStringValue(result.Frontmatter, "name")
918+
frontmatterName := extractStringFromMap(result.Frontmatter, "name", nil)
919919
if frontmatterName != "" {
920920
workflowName = frontmatterName
921921
}
@@ -954,7 +954,7 @@ func (c *Compiler) ParseWorkflowFile(markdownPath string) (*WorkflowData, error)
954954
ToolsStartupTimeout: toolsStartupTimeout,
955955
TrialMode: c.trialMode,
956956
TrialLogicalRepo: c.trialLogicalRepoSlug,
957-
GitHubToken: extractStringValue(result.Frontmatter, "github-token"),
957+
GitHubToken: extractStringFromMap(result.Frontmatter, "github-token", nil),
958958
StrictMode: c.strictMode,
959959
SecretMasking: secretMasking,
960960
}

pkg/workflow/config_helpers.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ func parseLabelsFromConfig(configMap map[string]any) []string {
3030
return nil
3131
}
3232

33-
// parseStringFromConfig is a generic helper that extracts and validates a string value from a config map
33+
// extractStringFromMap is a generic helper that extracts and validates a string value from a map
3434
// Returns the string value, or empty string if not present or invalid
35-
func parseStringFromConfig(configMap map[string]any, key string) string {
36-
if value, exists := configMap[key]; exists {
35+
// If log is provided, it will log the extracted value for debugging
36+
func extractStringFromMap(m map[string]any, key string, log *logger.Logger) string {
37+
if value, exists := m[key]; exists {
3738
if valueStr, ok := value.(string); ok {
38-
configHelpersLog.Printf("Parsed %s from config: %s", key, valueStr)
39+
if log != nil {
40+
log.Printf("Parsed %s from config: %s", key, valueStr)
41+
}
3942
return valueStr
4043
}
4144
}
@@ -45,13 +48,13 @@ func parseStringFromConfig(configMap map[string]any, key string) string {
4548
// parseTitlePrefixFromConfig extracts and validates title-prefix from a config map
4649
// Returns the title prefix string, or empty string if not present or invalid
4750
func parseTitlePrefixFromConfig(configMap map[string]any) string {
48-
return parseStringFromConfig(configMap, "title-prefix")
51+
return extractStringFromMap(configMap, "title-prefix", configHelpersLog)
4952
}
5053

5154
// parseTargetRepoFromConfig extracts the target-repo value from a config map.
5255
// Returns the target repository slug as a string, or empty string if not present or invalid.
5356
// This function does not perform any special handling or validation for wildcard values ("*");
5457
// callers are responsible for validating the returned value as needed.
5558
func parseTargetRepoFromConfig(configMap map[string]any) string {
56-
return parseStringFromConfig(configMap, "target-repo")
59+
return extractStringFromMap(configMap, "target-repo", configHelpersLog)
5760
}

pkg/workflow/config_parsing_helpers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55
)
66

7-
func TestParseStringFromConfig(t *testing.T) {
7+
func TestExtractStringFromMap(t *testing.T) {
88
tests := []struct {
99
name string
1010
input map[string]any
@@ -78,7 +78,7 @@ func TestParseStringFromConfig(t *testing.T) {
7878

7979
for _, tt := range tests {
8080
t.Run(tt.name, func(t *testing.T) {
81-
result := parseStringFromConfig(tt.input, tt.key)
81+
result := extractStringFromMap(tt.input, tt.key, nil)
8282
if result != tt.expected {
8383
t.Errorf("expected %q, got %q", tt.expected, result)
8484
}

pkg/workflow/frontmatter_helpers.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package workflow
22

3-
// extractStringValue extracts a string value from the frontmatter map
4-
func extractStringValue(frontmatter map[string]any, key string) string {
5-
value, exists := frontmatter[key]
6-
if !exists {
7-
return ""
8-
}
9-
10-
if strValue, ok := value.(string); ok {
11-
return strValue
12-
}
13-
14-
return ""
15-
}
16-
173
// parseIntValue safely parses various numeric types to int
184
// This is a common utility used across multiple parsing functions
195
func parseIntValue(value any) (int, bool) {

0 commit comments

Comments
 (0)