MCPcopy Create free account
hub / github.com/nginx-proxy/docker-gen / getFieldAsString

Function getFieldAsString

internal/template/sort.go:57–87  ·  view source on GitHub ↗
(item interface{}, path string)

Source from the content-addressed store, hash-verified

55}
56
57func getFieldAsString(item interface{}, path string) string {
58 // Mostly inspired by https://stackoverflow.com/a/47739620
59 e := deepGet(item, path)
60 r := reflect.ValueOf(e)
61
62 if r.Kind() == reflect.Invalid {
63 return ""
64 }
65
66 fieldValue := r.Interface()
67
68 switch v := fieldValue.(type) {
69 case int:
70 return strconv.FormatInt(int64(v), 10)
71 case int32:
72 return strconv.FormatInt(int64(v), 10)
73 case int64:
74 return strconv.FormatInt(v, 10)
75 case string:
76 return v
77 case bool:
78 if v {
79 return "true"
80 }
81 return "false"
82 case time.Time:
83 return v.String()
84 default:
85 return ""
86 }
87}
88
89// method required to implement sort.Interface
90func (s sortableByKey) Less(i, j int) bool {

Callers 2

LessMethod · 0.85
TestGetFieldAsStringFunction · 0.85

Calls 2

deepGetFunction · 0.85
StringMethod · 0.45

Tested by 1

TestGetFieldAsStringFunction · 0.68