github.com/ssube/gitlab-ci-multi-runner@v1.2.1-0.20160607142738-b8d1285632e6/common/build_options.go (about) 1 package common 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strings" 7 8 "gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers" 9 ) 10 11 type BuildOptions map[string]interface{} 12 13 func (m *BuildOptions) Get(keys ...string) (interface{}, bool) { 14 return helpers.GetMapKey(*m, keys...) 15 } 16 17 func (m *BuildOptions) GetSubOptions(keys ...string) (result BuildOptions, ok bool) { 18 value, ok := helpers.GetMapKey(*m, keys...) 19 if ok { 20 result, ok = value.(map[string]interface{}) 21 } 22 return 23 } 24 25 func (m *BuildOptions) GetString(keys ...string) (result string, ok bool) { 26 value, ok := helpers.GetMapKey(*m, keys...) 27 if ok { 28 result, ok = value.(string) 29 } 30 return 31 } 32 33 func (m *BuildOptions) Decode(result interface{}, keys ...string) error { 34 value, ok := m.Get(keys...) 35 if !ok { 36 return fmt.Errorf("key not found %v", strings.Join(keys, ".")) 37 } 38 39 data, err := json.Marshal(value) 40 if err != nil { 41 return err 42 } 43 44 return json.Unmarshal(data, result) 45 } 46 47 func convertMapToStringMap(in interface{}) (out interface{}, err error) { 48 mapString, ok := in.(map[string]interface{}) 49 if ok { 50 for k, v := range mapString { 51 mapString[k], err = convertMapToStringMap(v) 52 if err != nil { 53 return 54 } 55 } 56 return mapString, nil 57 } 58 59 mapInterface, ok := in.(map[interface{}]interface{}) 60 if ok { 61 mapString := make(map[string]interface{}) 62 for k, v := range mapInterface { 63 key, ok := k.(string) 64 if !ok { 65 return nil, fmt.Errorf("failed to convert %v to string", k) 66 } 67 68 mapString[key], err = convertMapToStringMap(v) 69 if err != nil { 70 return 71 } 72 } 73 return mapString, nil 74 } 75 76 return in, nil 77 } 78 79 func (m *BuildOptions) Sanitize() (err error) { 80 n := make(BuildOptions) 81 for k, v := range *m { 82 n[k], err = convertMapToStringMap(v) 83 if err != nil { 84 return 85 } 86 } 87 *m = n 88 return 89 }