Debian Codegraph @

pkg/format/enum_formatter.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 "stringsscip-go gomod github.com/golang/go/src . strings/"
5
6 "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/"
7 )
8
9 // EnumFormatter is the helper methods for enum-like types.
10 type EnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter# struct {
11 namesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#names. []string
12 firstOffsetscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#firstOffset. int
13 aliasesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#aliases. map[string]int
14 }
15
16 // EnumInvalid is the constant value that an enum gets assigned when it could not be parsed.
17 const EnumInvalidscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumInvalid. = -1
18
19 // CreateEnumFormatter creates a EnumFormatter struct.
20 func CreateEnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/CreateEnumFormatter().(names []string, optAliases ...map[string]int) 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# {
21 aliases := map[string]int{}
22 if len(optAliases) > 0 {
23 aliases = optAliases[0]
24 }
25
26 firstOffset := 0
27
28 for i, name := range names {
29 if name != "" {
30 firstOffset = i
31
32 break
33 }
34 }
35
36 return &EnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#{
37 names,
38 firstOffset,
39 aliases,
40 }
41 }
42
43 // Names is the list of the valid Enum string values.
44 func (ef EnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#) Namesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#Names().() []string {
45 return ef.namesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#names.[ef.firstOffsetscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#firstOffset.:]
46 }
47
48 // Parse takes an enum mapped string and returns it's int representation or EnumInvalid (-1).
49 func (ef EnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#) Parsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#Parse().(mappedString string) int {
50 target := stringsscip-go gomod github.com/golang/go/src . strings/.ToLowerscip-go gomod github.com/golang/go/src . strings/ToLower().(mappedString)
51 for index, name := range ef.namesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#names. {
52 if stringsscip-go gomod github.com/golang/go/src . strings/.EqualFoldscip-go gomod github.com/golang/go/src . strings/EqualFold().(target, name) {
53 return index
54 }
55 }
56
57 if index, found := ef.aliasesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#aliases.[mappedString]; found {
58 return index
59 }
60
61 return EnumInvalidscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumInvalid.
62 }
63
64 // Print takes a enum mapped int and returns it's string representation or "Invalid".
65 func (ef EnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#) Printscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#Print().(e int) string {
66 if e >= len(ef.namesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#names.) || e < 0 {
67 return "Invalid"
68 }
69
70 return ef.namesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EnumFormatter#names.[e]
71 }