MCPcopy
hub / github.com/cortexlabs/cortex / strIndent

Function strIndent

pkg/lib/strings/stringify.go:155–334  ·  view source on GitHub ↗

This is similar to json.Marshal, but handles non-string keys (which we support). It should be valid YAML since we use it in templates

(val interface{}, indent string, currentIndent string, newlineChar string, quoteStr string)

Source from the content-addressed store, hash-verified

153
154// This is similar to json.Marshal, but handles non-string keys (which we support). It should be valid YAML since we use it in templates
155func strIndent(val interface{}, indent string, currentIndent string, newlineChar string, quoteStr string) string {
156 if val == nil {
157 return "<null>"
158 }
159
160 value := reflect.ValueOf(val)
161 valueType := value.Type()
162
163 if value.Kind() == reflect.Invalid {
164 return "<invalid>"
165 }
166
167 if value.Kind() == reflect.Chan || value.Kind() == reflect.Func || value.Kind() == reflect.Interface || value.Kind() == reflect.Map || value.Kind() == reflect.Ptr || value.Kind() == reflect.Slice {
168 if value.IsNil() {
169 return "<null>"
170 }
171 }
172
173 stringSep := "," + newlineChar
174 if len(newlineChar) == 0 {
175 stringSep += " "
176 }
177
178 // Use a String() method if one exists
179 funcVal := value.MethodByName("String")
180 if funcVal.IsValid() {
181 t := funcVal.Type()
182 if t.NumIn() == 0 && t.NumOut() == 1 && t.Out(0).Kind() == reflect.String {
183 return strIndent(funcVal.Call(nil)[0].Interface().(string), indent, currentIndent, newlineChar, quoteStr)
184 }
185 }
186 if _, ok := reflect.PtrTo(valueType).MethodByName("String"); ok {
187 ptrValue := reflect.New(valueType)
188 ptrValue.Elem().Set(value)
189 funcVal := ptrValue.MethodByName("String")
190 if funcVal.IsValid() {
191 t := funcVal.Type()
192 if t.NumIn() == 0 && t.NumOut() == 1 && t.Out(0).Kind() == reflect.String {
193 return strIndent(funcVal.Call(nil)[0].Interface().(string), indent, currentIndent, newlineChar, quoteStr)
194 }
195 }
196 }
197
198 switch value.Kind() {
199
200 case reflect.Bool:
201 var t bool
202 return Bool(value.Convert(reflect.TypeOf(t)).Interface().(bool))
203 case reflect.Float32:
204 var t float32
205 return Float32(value.Convert(reflect.TypeOf(t)).Interface().(float32))
206 case reflect.Float64:
207 var t float64
208 return Float64(value.Convert(reflect.TypeOf(t)).Interface().(float64))
209 case reflect.Int:
210 var t int
211 return Int(value.Convert(reflect.TypeOf(t)).Interface().(int))
212 case reflect.Int8:

Callers 5

strIndentValueFunction · 0.85
ObjFunction · 0.85
ObjFlatFunction · 0.85
ObjFlatNoQuotesFunction · 0.85
UserStrFunction · 0.85

Calls 15

UintFunction · 0.85
Uint8Function · 0.85
Uint16Function · 0.85
Uint32Function · 0.85
Uint64Function · 0.85
Complex64Function · 0.85
Complex128Function · 0.85
UintptrFunction · 0.85
strIndentValueFunction · 0.85
TypeMethod · 0.80
SetMethod · 0.80
BoolFunction · 0.70

Tested by

no test coverage detected