Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions sdk/framework/field_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,23 @@ func (d *FieldData) getPrimitive(k string, schema *FieldSchema) (interface{}, bo
if err := mapstructure.WeakDecode(raw, &listResult); err != nil {
return nil, false, err
}
if len(listResult) == 1 {
// Try splitting by comma to see if there are multiple pairs
commaSplit := strings.Split(listResult[0], ",")
if len(commaSplit) > 1 {
// Multiple pairs found, use the comma-split version
listResult = commaSplit
}
}

result := make(map[string]string, len(listResult))
for _, keyPair := range listResult {
// Trim whitespace from each pair
keyPair = strings.TrimSpace(keyPair)
if keyPair == "" {
continue
}

keyPairSlice := strings.SplitN(keyPair, "=", 2)
if len(keyPairSlice) != 2 || keyPairSlice[0] == "" {
return nil, false, fmt.Errorf("invalid key pair %q", keyPair)
Expand Down
78 changes: 78 additions & 0 deletions sdk/framework/field_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1275,3 +1275,81 @@ func TestValidateStrict(t *testing.T) {
})
}
}

func TestTypeKVPairs(t *testing.T) {
fields := map[string]*FieldSchema{
"metadata": {
Type: TypeKVPairs,
},
}

testCases := []struct {
name string
input string
expectedCount int
expectedPairs map[string]string
}{
{
name: "Single pair",
input: "key1=value1",
expectedCount: 1,
expectedPairs: map[string]string{"key1": "value1"},
},
{
name: "Multiple pairs",
input: "key1=value1,key2=value2,key3=value3",
expectedCount: 3,
expectedPairs: map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
},
},
{
name: "Two pairs",
input: "key1=value1,key2=value2",
expectedCount: 2,
expectedPairs: map[string]string{
"key1": "value1",
"key2": "value2",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

rawData := map[string]interface{}{
"metadata": tc.input,
}

fieldData := &FieldData{
Raw: rawData,
Schema: fields,
}

result := fieldData.Get("metadata")
kvMap, ok := result.(map[string]string)

if !ok {
t.Fatal("Failed to convert to map[string]string")
}

if len(kvMap) != tc.expectedCount {
t.Errorf("Expected %d pairs, got %d", tc.expectedCount, len(kvMap))
t.Logf("Actual map: %v", kvMap)
}

for expectedKey, expectedValue := range tc.expectedPairs {
actualValue, exists := kvMap[expectedKey]
if !exists {
t.Errorf("Expected key '%s' not found in result", expectedKey)
} else if actualValue != expectedValue {
t.Errorf("For key '%s': expected value '%s', got '%s'",
expectedKey, expectedValue, actualValue)

}
}
})
}
}