diff --git a/sdk/framework/field_data.go b/sdk/framework/field_data.go index a1a27a83b25..41124641928 100644 --- a/sdk/framework/field_data.go +++ b/sdk/framework/field_data.go @@ -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) diff --git a/sdk/framework/field_data_test.go b/sdk/framework/field_data_test.go index 68fc57e316c..89233db9a08 100644 --- a/sdk/framework/field_data_test.go +++ b/sdk/framework/field_data_test.go @@ -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) + + } + } + }) + } +}