|
1 | | -// This file provides generic map and type conversion utilities. |
| 1 | +// This file provides generic map utilities. |
2 | 2 | // |
3 | 3 | // This file contains low-level helper functions for working with map[string]any |
4 | | -// structures and type conversions. These utilities are used throughout the workflow |
5 | | -// compilation process to safely parse and manipulate configuration data. |
| 4 | +// structures. These utilities are used throughout the workflow compilation process |
| 5 | +// to safely parse and manipulate configuration data. |
6 | 6 | // |
7 | 7 | // # Organization Rationale |
8 | 8 | // |
9 | 9 | // These functions are grouped in a helper file because they: |
10 | 10 | // - Provide generic, reusable utilities (used by 10+ files) |
11 | | -// - Have no specific domain focus (work with any map/type data) |
| 11 | +// - Have no specific domain focus (work with any map data) |
12 | 12 | // - Are small, stable functions (< 50 lines each) |
13 | 13 | // - Follow clear, single-purpose patterns |
14 | 14 | // |
15 | 15 | // This follows the helper file conventions documented in skills/developer/SKILL.md. |
16 | 16 | // |
17 | 17 | // # Key Functions |
18 | 18 | // |
19 | | -// Type Conversion (delegated to pkg/typeutil for general-purpose reuse): |
20 | | -// - parseIntValue() - Strictly parse numeric types to int; returns (value, ok). Use when |
21 | | -// the caller needs to distinguish "missing/invalid" from a zero value, or when string |
22 | | -// inputs are not expected (e.g. YAML config field parsing). Delegates to typeutil.ParseIntValue. |
23 | | -// - safeUint64ToInt() - Convert uint64 to int, returning 0 on overflow. Delegates to typeutil.SafeUint64ToInt. |
24 | | -// - safeUintToInt() - Convert uint to int, returning 0 on overflow. Delegates to typeutil.SafeUintToInt. |
25 | | -// - ConvertToInt() - Leniently convert any value (int/int64/float64/string) to int, returning 0 |
26 | | -// on failure. Use when the input may come from heterogeneous sources such as JSON metrics, |
27 | | -// log parsing, or user-provided strings where a zero default on failure is acceptable. |
28 | | -// Delegates to typeutil.ConvertToInt. |
29 | | -// - ConvertToFloat() - Safely convert any value (float64/int/int64/string) to float64. |
30 | | -// Delegates to typeutil.ConvertToFloat. |
31 | | -// |
32 | 19 | // Map Operations: |
33 | 20 | // - excludeMapKeys() - Create new map excluding specified keys |
34 | 21 | // - sortedMapKeys() - Return sorted keys of a map[string]string |
35 | 22 | // |
36 | | -// These utilities handle common type conversion and map manipulation patterns that |
37 | | -// occur frequently during YAML-to-struct parsing and configuration processing. |
38 | | - |
39 | | -package workflow |
40 | | - |
41 | | -import ( |
42 | | - "sort" |
43 | | - |
44 | | - "github.com/github/gh-aw/pkg/typeutil" |
45 | | -) |
46 | | - |
47 | | -// parseIntValue strictly parses numeric types to int, returning (value, true) on success |
48 | | -// and (0, false) for any unrecognized or non-numeric type. |
49 | | -// |
50 | | -// Use this when the caller needs to distinguish a missing/invalid value from a legitimate |
51 | | -// zero, or when string inputs are not expected (e.g. YAML config field parsing where the |
52 | | -// YAML library has already produced a typed numeric value). |
53 | | -// |
54 | | -// For lenient conversion that also handles string inputs and returns 0 on failure, use |
55 | | -// ConvertToInt instead. |
| 23 | +// For type conversion utilities, use pkg/typeutil directly: |
| 24 | +// - typeutil.ParseIntValue() - Strictly parse numeric types to int; returns (value, ok). |
| 25 | +// - typeutil.SafeUint64ToInt() - Convert uint64 to int, returning 0 on overflow. |
| 26 | +// - typeutil.SafeUintToInt() - Convert uint to int, returning 0 on overflow. |
| 27 | +// - typeutil.ConvertToInt() - Leniently convert any value to int, returning 0 on failure. |
| 28 | +// - typeutil.ConvertToFloat() - Safely convert any value to float64. |
| 29 | +// - typeutil.ParseBool() - Extract a bool from map[string]any by key. |
56 | 30 | // |
57 | | -// This is a package-private alias for typeutil.ParseIntValue. |
58 | | -func parseIntValue(value any) (int, bool) { return typeutil.ParseIntValue(value) } |
| 31 | +// These utilities handle common map manipulation patterns that occur frequently |
| 32 | +// during YAML-to-struct parsing and configuration processing. |
59 | 33 |
|
60 | | -// safeUint64ToInt safely converts uint64 to int, returning 0 if overflow would occur. |
61 | | -// This is a package-private alias for typeutil.SafeUint64ToInt. |
62 | | -func safeUint64ToInt(u uint64) int { return typeutil.SafeUint64ToInt(u) } |
| 34 | +package workflow |
63 | 35 |
|
64 | | -// safeUintToInt safely converts uint to int, returning 0 if overflow would occur. |
65 | | -// This is a package-private alias for typeutil.SafeUintToInt. |
66 | | -func safeUintToInt(u uint) int { return typeutil.SafeUintToInt(u) } |
| 36 | +import "sort" |
67 | 37 |
|
68 | 38 | // excludeMapKeys creates a new map excluding the specified keys |
69 | 39 | func excludeMapKeys(original map[string]any, excludeKeys ...string) map[string]any { |
@@ -91,26 +61,3 @@ func sortedMapKeys(m map[string]string) []string { |
91 | 61 | sort.Strings(keys) |
92 | 62 | return keys |
93 | 63 | } |
94 | | - |
95 | | -// ConvertToInt leniently converts any value to int, returning 0 on failure. |
96 | | -// |
97 | | -// Unlike parseIntValue, this function also handles string inputs via strconv.Atoi, |
98 | | -// making it suitable for heterogeneous sources such as JSON metrics, log-parsed data, |
99 | | -// or user-provided configuration where a zero default on failure is acceptable and |
100 | | -// the caller does not need to distinguish "invalid" from a genuine zero. |
101 | | -// |
102 | | -// For strict numeric-only parsing where the caller must distinguish missing/invalid |
103 | | -// values from zero, use parseIntValue instead. |
104 | | -// |
105 | | -// This is a workflow-package alias for typeutil.ConvertToInt. For new code outside |
106 | | -// this package, prefer using typeutil.ConvertToInt directly. |
107 | | -func ConvertToInt(val any) int { return typeutil.ConvertToInt(val) } |
108 | | - |
109 | | -// ConvertToFloat safely converts any value to float64, returning 0 on failure. |
110 | | -// |
111 | | -// Supported input types: float64, int, int64, and string (parsed via strconv.ParseFloat). |
112 | | -// Returns 0 for any other type or for strings that cannot be parsed as a float. |
113 | | -// |
114 | | -// This is a workflow-package alias for typeutil.ConvertToFloat. For new code outside |
115 | | -// this package, prefer using typeutil.ConvertToFloat directly. |
116 | | -func ConvertToFloat(val any) float64 { return typeutil.ConvertToFloat(val) } |
0 commit comments