github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/scalar/scalar.go (about) 1 package scalar 2 3 import ( 4 "encoding/json" 5 "io" 6 7 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 8 9 "github.com/pkg/errors" 10 ) 11 12 // WriteMarshalled missing godoc 13 func WriteMarshalled(in interface{}, w io.Writer) error { 14 bytes, err := json.Marshal(in) 15 if err != nil { 16 return errors.Errorf("error with marshalling %T", in) 17 } 18 19 _, err = w.Write(bytes) 20 if err != nil { 21 return errors.Errorf("error with writing %T", in) 22 } 23 return nil 24 } 25 26 // ConvertToString missing godoc 27 func ConvertToString(in interface{}) (string, error) { 28 if in == nil { 29 return "", apperrors.NewInvalidDataError("input should not be nil") 30 } 31 32 value, ok := in.(string) 33 if !ok { 34 ptr, ok := in.(*string) 35 if !ok { 36 return "", errors.Errorf("unexpected input type: %T, should be string", in) 37 } 38 39 value = *ptr 40 } 41 42 return value, nil 43 } 44 45 // ConvertToMapStringStringArray missing godoc 46 func ConvertToMapStringStringArray(in interface{}) (map[string][]string, error) { 47 if in == nil { 48 return nil, apperrors.NewInvalidDataError("input should not be nil") 49 } 50 51 result := make(map[string][]string) 52 53 value, ok := in.(map[string][]string) 54 if !ok { 55 return nil, errors.Errorf("unexpected input type: %T, should be map[string][]string", in) 56 } 57 58 for k, v := range value { 59 var strValues []string 60 for _, item := range v { 61 str, ok := interface{}(item).(string) 62 if !ok { 63 return nil, errors.Errorf("value `%+v` must be a string, not %T", item, item) 64 } 65 66 strValues = append(strValues, str) 67 } 68 69 result[k] = strValues 70 } 71 return result, nil 72 }