MCPcopy Create free account
hub / github.com/compose-spec/compose-go / convertToStringKeysRecursive

Function convertToStringKeysRecursive

loader/loader.go:873–924  ·  view source on GitHub ↗

keys need to be converted to strings for jsonschema

(value interface{}, keyPrefix string)

Source from the content-addressed store, hash-verified

871
872// keys need to be converted to strings for jsonschema
873func convertToStringKeysRecursive(value interface{}, keyPrefix string) (interface{}, error) {
874 if mapping, ok := value.(map[string]interface{}); ok {
875 for key, entry := range mapping {
876 var newKeyPrefix string
877 if keyPrefix == "" {
878 newKeyPrefix = key
879 } else {
880 newKeyPrefix = fmt.Sprintf("%s.%s", keyPrefix, key)
881 }
882 convertedEntry, err := convertToStringKeysRecursive(entry, newKeyPrefix)
883 if err != nil {
884 return nil, err
885 }
886 mapping[key] = convertedEntry
887 }
888 return mapping, nil
889 }
890 if mapping, ok := value.(map[interface{}]interface{}); ok {
891 dict := make(map[string]interface{})
892 for key, entry := range mapping {
893 str, ok := key.(string)
894 if !ok {
895 return nil, formatInvalidKeyError(keyPrefix, key)
896 }
897 var newKeyPrefix string
898 if keyPrefix == "" {
899 newKeyPrefix = str
900 } else {
901 newKeyPrefix = fmt.Sprintf("%s.%s", keyPrefix, str)
902 }
903 convertedEntry, err := convertToStringKeysRecursive(entry, newKeyPrefix)
904 if err != nil {
905 return nil, err
906 }
907 dict[str] = convertedEntry
908 }
909 return dict, nil
910 }
911 if list, ok := value.([]interface{}); ok {
912 var convertedList []interface{}
913 for index, entry := range list {
914 newKeyPrefix := fmt.Sprintf("%s[%d]", keyPrefix, index)
915 convertedEntry, err := convertToStringKeysRecursive(entry, newKeyPrefix)
916 if err != nil {
917 return nil, err
918 }
919 convertedList = append(convertedList, convertedEntry)
920 }
921 return convertedList, nil
922 }
923 return value, nil
924}
925
926func formatInvalidKeyError(keyPrefix string, key interface{}) error {
927 var location string

Callers 1

loadYamlFileFunction · 0.85

Calls 1

formatInvalidKeyErrorFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…