Debian Codegraph @

pkg/format/field_info.go

raw symbols SCIP
1 package formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/
2
3 import (
4 "reflectscip-go gomod github.com/golang/go/src . reflect/"
5 "slicesscip-go gomod github.com/golang/go/src . slices/"
6 "strconvscip-go gomod github.com/golang/go/src . strconv/"
7 "stringsscip-go gomod github.com/golang/go/src . strings/"
8
9 "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/"
10 "github.com/nicholas-fedor/shoutrrr/pkg/utilscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/"
11 )
12
13 // FieldInfo is the meta data about a config field.
14 type FieldInfoscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo# struct {
15 Namescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Name. string
16 Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Type. reflectscip-go gomod github.com/golang/go/src . reflect/.Typescip-go gomod github.com/golang/go/src . reflect/Type#
17 EnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#EnumFormatter. typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.EnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/EnumFormatter#
18 Descriptionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Description. string
19 DefaultValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#DefaultValue. string
20 Templatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Template. string
21 Requiredscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Required. bool
22 URLPartsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#URLParts. []URLPartscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/URLPart#
23 Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Title. bool
24 Basescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Base. int
25 Keysscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Keys. []string
26 ItemSeparatorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#ItemSeparator. rune
27 }
28
29 // DefaultBase represents the default numeric base (decimal) for fields.
30 const DefaultBasescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/DefaultBase. = 10
31
32 // IsEnum returns whether a EnumFormatter has been assigned to the field and that it is of a suitable type.
33 func (fi *FieldInfoscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#) IsEnumscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#IsEnum().() bool {
34 return fi.EnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#EnumFormatter. != nil && fi.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Type..Kindscip-go gomod github.com/golang/go/src . reflect/Type#Kind().() == reflectscip-go gomod github.com/golang/go/src . reflect/.Intscip-go gomod github.com/golang/go/src . reflect/Int.
35 }
36
37 // IsURLPart returns whether the field is serialized as the specified part of an URL.
38 func (fi *FieldInfoscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#) IsURLPartscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#IsURLPart().(part URLPartscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/URLPart#) bool {
39 return slicesscip-go gomod github.com/golang/go/src . slices/.Containsscip-go gomod github.com/golang/go/src . slices/Contains().(fi.URLPartsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#URLParts., part)
40 }
41
42 func getStructFieldInfoscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/getStructFieldInfo().(structType reflectscip-go gomod github.com/golang/go/src . reflect/.Typescip-go gomod github.com/golang/go/src . reflect/Type#, enums map[string]typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.EnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/EnumFormatter#) []FieldInfoscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo# {
43 numFields := structType.NumFieldscip-go gomod github.com/golang/go/src . reflect/Type#NumField().()
44 fields := make([]FieldInfoscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#, 0, numFields)
45 maxKeyLen := 0
46
47 for i := range numFields {
48 fieldDef := structType.Fieldscip-go gomod github.com/golang/go/src . reflect/Type#Field().(i)
49
50 if isHiddenFieldscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/isHiddenField().(&fieldDef) {
51 // This is an embedded or private field, which should not be part of the Config output
52 continue
53 }
54
55 //nolint:exhaustruct_v5 // Partial initialization is intentional, other fields are set below
56 info := FieldInfoscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#{
57 Namescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Name.: fieldDef.Namescip-go gomod github.com/golang/go/src . reflect/StructField#Name.,
58 Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Type.: fieldDef.Typescip-go gomod github.com/golang/go/src . reflect/StructField#Type.,
59 Requiredscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Required.: true,
60 Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Title.: false,
61 ItemSeparatorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#ItemSeparator.: ',',
62 }
63
64 if utilscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/.IsNumericscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/IsNumeric().(fieldDef.Typescip-go gomod github.com/golang/go/src . reflect/StructField#Type..Kindscip-go gomod github.com/golang/go/src . reflect/Type#Kind().()) {
65 info.Basescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Base. = getFieldBasescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/getFieldBase().(&fieldDef)
66 }
67
68 if tag, ok := fieldDef.Tagscip-go gomod github.com/golang/go/src . reflect/StructField#Tag..Lookupscip-go gomod github.com/golang/go/src . reflect/StructTag#Lookup().("desc"); ok {
69 info.Descriptionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Description. = tag
70 }
71
72 if tag, ok := fieldDef.Tagscip-go gomod github.com/golang/go/src . reflect/StructField#Tag..Lookupscip-go gomod github.com/golang/go/src . reflect/StructTag#Lookup().("tpl"); ok {
73 info.Templatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Template. = tag
74 }
75
76 if tag, ok := fieldDef.Tagscip-go gomod github.com/golang/go/src . reflect/StructField#Tag..Lookupscip-go gomod github.com/golang/go/src . reflect/StructTag#Lookup().("default"); ok {
77 info.Requiredscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Required. = false
78 info.DefaultValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#DefaultValue. = tag
79 }
80
81 if _, ok := fieldDef.Tagscip-go gomod github.com/golang/go/src . reflect/StructField#Tag..Lookupscip-go gomod github.com/golang/go/src . reflect/StructTag#Lookup().("optional"); ok {
82 info.Requiredscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Required. = false
83 }
84
85 if _, ok := fieldDef.Tagscip-go gomod github.com/golang/go/src . reflect/StructField#Tag..Lookupscip-go gomod github.com/golang/go/src . reflect/StructTag#Lookup().("title"); ok {
86 info.Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Title. = true
87 }
88
89 if tag, ok := fieldDef.Tagscip-go gomod github.com/golang/go/src . reflect/StructField#Tag..Lookupscip-go gomod github.com/golang/go/src . reflect/StructTag#Lookup().("url"); ok {
90 info.URLPartsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#URLParts. = ParseURLPartsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ParseURLParts().(tag)
91 }
92
93 if tag, ok := fieldDef.Tagscip-go gomod github.com/golang/go/src . reflect/StructField#Tag..Lookupscip-go gomod github.com/golang/go/src . reflect/StructTag#Lookup().("key"); ok {
94 tag := stringsscip-go gomod github.com/golang/go/src . strings/.ToLowerscip-go gomod github.com/golang/go/src . strings/ToLower().(tag)
95 info.Keysscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Keys. = stringsscip-go gomod github.com/golang/go/src . strings/.Splitscip-go gomod github.com/golang/go/src . strings/Split().(tag, ",")
96 }
97
98 if tag, ok := fieldDef.Tagscip-go gomod github.com/golang/go/src . reflect/StructField#Tag..Lookupscip-go gomod github.com/golang/go/src . reflect/StructTag#Lookup().("sep"); ok {
99 info.ItemSeparatorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#ItemSeparator. = rune(tag[0])
100 }
101
102 if ef, isEnum := enums[fieldDef.Namescip-go gomod github.com/golang/go/src . reflect/StructField#Name.]; isEnum {
103 info.EnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#EnumFormatter. = ef
104 }
105
106 fields = append(fields, info)
107
108 keyLen := len(fieldDef.Namescip-go gomod github.com/golang/go/src . reflect/StructField#Name.)
109 if keyLen > maxKeyLen {
110 maxKeyLen = keyLen
111 }
112 }
113
114 return fields
115 }
116
117 func isHiddenFieldscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/isHiddenField().(field *reflectscip-go gomod github.com/golang/go/src . reflect/.StructFieldscip-go gomod github.com/golang/go/src . reflect/StructField#) bool {
118 return field.Anonymousscip-go gomod github.com/golang/go/src . reflect/StructField#Anonymous. || stringsscip-go gomod github.com/golang/go/src . strings/.ToUpperscip-go gomod github.com/golang/go/src . strings/ToUpper().(field.Namescip-go gomod github.com/golang/go/src . reflect/StructField#Name.[0:1]) != field.Namescip-go gomod github.com/golang/go/src . reflect/StructField#Name.[0:1]
119 }
120
121 func getFieldBasescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/getFieldBase().(field *reflectscip-go gomod github.com/golang/go/src . reflect/.StructFieldscip-go gomod github.com/golang/go/src . reflect/StructField#) int {
122 if tag, ok := field.Tagscip-go gomod github.com/golang/go/src . reflect/StructField#Tag..Lookupscip-go gomod github.com/golang/go/src . reflect/StructTag#Lookup().("base"); ok {
123 if base, err := strconvscip-go gomod github.com/golang/go/src . strconv/.ParseUintscip-go gomod github.com/golang/go/src . strconv/ParseUint().(tag, 10, 8); err == nil {
124 return int(base)
125 }
126 }
127
128 // Default to base 10 if not tagged
129 return DefaultBasescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/DefaultBase.
130 }