| 1 |
package format |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"slices" |
| 6 |
"strconv" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 10 |
"github.com/nicholas-fedor/shoutrrr/pkg/util" |
| 11 |
) |
| 12 |
|
| 13 |
// FieldInfo is the meta data about a config field. |
| 14 |
type FieldInfo struct { |
| 15 |
Name string |
| 16 |
Type reflect.Type |
| 17 |
EnumFormatter types.EnumFormatter |
| 18 |
Description string |
| 19 |
DefaultValue string |
| 20 |
Template string |
| 21 |
Required bool |
| 22 |
URLParts []URLPart |
| 23 |
Title bool |
| 24 |
Base int |
| 25 |
Keys []string |
| 26 |
ItemSeparator rune |
| 27 |
} |
| 28 |
|
| 29 |
// DefaultBase represents the default numeric base (decimal) for fields. |
| 30 |
const 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 *FieldInfo) IsEnum() bool { |
| 34 |
return fi.EnumFormatter != nil && fi.Type.Kind() == reflect.Int |
| 35 |
} |
| 36 |
|
| 37 |
// IsURLPart returns whether the field is serialized as the specified part of an URL. |
| 38 |
func (fi *FieldInfo) IsURLPart(part URLPart) bool { |
| 39 |
return slices.Contains(fi.URLParts, part) |
| 40 |
} |
| 41 |
|
| 42 |
func getStructFieldInfo(structType reflect.Type, enums map[string]types.EnumFormatter) []FieldInfo { |
| 43 |
numFields := structType.NumField() |
| 44 |
fields := make([]FieldInfo, 0, numFields) |
| 45 |
maxKeyLen := 0 |
| 46 |
|
| 47 |
for i := range numFields { |
| 48 |
fieldDef := structType.Field(i) |
| 49 |
|
| 50 |
if 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 // Partial initialization is intentional, other fields are set below |
| 56 |
info := FieldInfo{ |
| 57 |
Name: fieldDef.Name, |
| 58 |
Type: fieldDef.Type, |
| 59 |
Required: true, |
| 60 |
Title: false, |
| 61 |
ItemSeparator: ',', |
| 62 |
} |
| 63 |
|
| 64 |
if util.IsNumeric(fieldDef.Type.Kind()) { |
| 65 |
info.Base = getFieldBase(&fieldDef) |
| 66 |
} |
| 67 |
|
| 68 |
if tag, ok := fieldDef.Tag.Lookup("desc"); ok { |
| 69 |
info.Description = tag |
| 70 |
} |
| 71 |
|
| 72 |
if tag, ok := fieldDef.Tag.Lookup("tpl"); ok { |
| 73 |
info.Template = tag |
| 74 |
} |
| 75 |
|
| 76 |
if tag, ok := fieldDef.Tag.Lookup("default"); ok { |
| 77 |
info.Required = false |
| 78 |
info.DefaultValue = tag |
| 79 |
} |
| 80 |
|
| 81 |
if _, ok := fieldDef.Tag.Lookup("optional"); ok { |
| 82 |
info.Required = false |
| 83 |
} |
| 84 |
|
| 85 |
if _, ok := fieldDef.Tag.Lookup("title"); ok { |
| 86 |
info.Title = true |
| 87 |
} |
| 88 |
|
| 89 |
if tag, ok := fieldDef.Tag.Lookup("url"); ok { |
| 90 |
info.URLParts = ParseURLParts(tag) |
| 91 |
} |
| 92 |
|
| 93 |
if tag, ok := fieldDef.Tag.Lookup("key"); ok { |
| 94 |
tag := strings.ToLower(tag) |
| 95 |
info.Keys = strings.Split(tag, ",") |
| 96 |
} |
| 97 |
|
| 98 |
if tag, ok := fieldDef.Tag.Lookup("sep"); ok { |
| 99 |
info.ItemSeparator = rune(tag[0]) |
| 100 |
} |
| 101 |
|
| 102 |
if ef, isEnum := enums[fieldDef.Name]; isEnum { |
| 103 |
info.EnumFormatter = ef |
| 104 |
} |
| 105 |
|
| 106 |
fields = append(fields, info) |
| 107 |
|
| 108 |
keyLen := len(fieldDef.Name) |
| 109 |
if keyLen > maxKeyLen { |
| 110 |
maxKeyLen = keyLen |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return fields |
| 115 |
} |
| 116 |
|
| 117 |
func isHiddenField(field *reflect.StructField) bool { |
| 118 |
return field.Anonymous || strings.ToUpper(field.Name[0:1]) != field.Name[0:1] |
| 119 |
} |
| 120 |
|
| 121 |
func getFieldBase(field *reflect.StructField) int { |
| 122 |
if tag, ok := field.Tag.Lookup("base"); ok { |
| 123 |
if base, err := strconv.ParseUint(tag, 10, 8); err == nil { |
| 124 |
return int(base) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
// Default to base 10 if not tagged |
| 129 |
return DefaultBase |
| 130 |
} |