github.com/metux/go-metabuild@v0.0.0-20240118143255-d9ed5ab697f9/util/specobj/entries.go (about) 1 package specobj 2 3 import ( 4 magic "github.com/metux/go-magicdict" 5 ) 6 7 func (so SpecObj) EntryBoolDef(k Key, dflt bool) bool { 8 return magic.EntryBoolDef(so.Spec, k, dflt) 9 } 10 11 func (so SpecObj) EntryPutBool(k Key, v bool) error { 12 return magic.EntryPutBool(so.Spec, k, v) 13 } 14 15 func (so SpecObj) EntryIntDef(k Key, dflt int) int { 16 return magic.EntryIntDef(so.Spec, k, dflt) 17 } 18 19 func (so SpecObj) EntryPutInt(k Key, v int) error { 20 return magic.EntryPutInt(so.Spec, k, v) 21 } 22 23 func (so SpecObj) EntryStr(k Key) string { 24 return magic.EntryStr(so.Spec, k) 25 } 26 27 func (so SpecObj) EntryStrList(k Key) []string { 28 return magic.EntryStrList(so.Spec, k) 29 } 30 31 func (so SpecObj) EntryPutStrList(k Key, s []string) error { 32 return magic.EntryPutStrList(so.Spec, k, s) 33 } 34 35 func (so SpecObj) EntryPutStr(k Key, v string) error { 36 return magic.EntryPutStr(so.Spec, k, v) 37 } 38 39 func (so SpecObj) EntryPutStrMap(k Key, v map[string]string) error { 40 return magic.EntryPutStrMap(so.Spec, k, v) 41 } 42 43 func (so SpecObj) EntryDelete(k Key) { 44 magic.EntryDelete(so.Spec, k) 45 } 46 47 func (so SpecObj) EntryStrListAppend(k Key, s string) { 48 magic.EntryStrListAppend(so.Spec, k, s) 49 } 50 51 func (so SpecObj) EntryStrListAppendList(k Key, s []string) { 52 for _, x := range s { 53 magic.EntryStrListAppend(so.Spec, k, x) 54 } 55 } 56 57 func (so SpecObj) EntryKeys(k Key) []Key { 58 return magic.EntryKeys(so.Spec, k) 59 } 60 61 func (so SpecObj) EntryElems(k Key) []Entry { 62 return magic.EntryElems(so.Spec, k) 63 } 64 65 func (so SpecObj) EntryStrMap(k Key) map[Key]string { 66 return magic.EntryStrMap(so.Spec, k) 67 } 68 69 func (so SpecObj) EntryMap(k Key) map[Key]Entry { 70 return magic.EntryMap(so.Spec, k) 71 } 72 73 func (so SpecObj) EntrySpec(k Key) SpecObj { 74 if sub, err := magic.EntryMakeDict(so.Spec, k); err != nil { 75 return NewSpecObjErr(sub, err) 76 } else { 77 return NewSpecObj(sub) 78 } 79 } 80 81 func (so SpecObj) EntrySpecMap(k Key) map[Key]SpecObj { 82 m := make(map[Key]SpecObj) 83 for k, v := range so.EntryMap(k) { 84 m[k] = NewSpecObj(v) 85 } 86 return m 87 } 88 89 func (so SpecObj) EntrySpecList(k Key) []SpecObj { 90 elems := so.EntryElems(k) 91 ret := make([]SpecObj, len(elems)) 92 for idx, ent := range elems { 93 ret[idx] = NewSpecObj(ent) 94 } 95 return ret 96 } 97 98 func (so SpecObj) RequiredEntryStr(k Key) string { 99 return magic.RequiredEntryStr(so.Spec, k) 100 } 101 102 func (so SpecObj) RequiredEntryStrList(k Key) []string { 103 s := magic.EntryStrList(so.Spec, k) 104 if len(s) == 0 { 105 panic("required at least one element for key: " + string(k)) 106 } 107 return s 108 } 109 110 func SpecXformList[K ~string, V any](so SpecObj, k Key, proc func(K) V) []V { 111 names := so.EntryStrList(k) 112 data := make([]V, len(names), len(names)) 113 for idx, name := range names { 114 data[idx] = proc(K(name)) 115 } 116 return data 117 } 118 119 func SpecXformSpecList[V any](so SpecObj, k Key, proc func(SpecObj) V) []V { 120 vals := so.EntrySpecList(k) 121 data := make([]V, len(vals), len(vals)) 122 for idx, val := range vals { 123 data[idx] = proc(val) 124 } 125 return data 126 }