mirror of
https://github.com/safing/portbase
synced 2025-09-01 18:19:57 +00:00
Add Has fn to config perspective
This commit is contained in:
parent
8a0c3a077c
commit
dbbe556f3c
2 changed files with 10 additions and 1 deletions
|
@ -16,6 +16,7 @@ type OptionType uint8
|
||||||
|
|
||||||
// Various attribute options. Use ExternalOptType for extended types in the frontend.
|
// Various attribute options. Use ExternalOptType for extended types in the frontend.
|
||||||
const (
|
const (
|
||||||
|
optTypeAny OptionType = 0
|
||||||
OptTypeString OptionType = 1
|
OptTypeString OptionType = 1
|
||||||
OptTypeStringArray OptionType = 2
|
OptTypeStringArray OptionType = 2
|
||||||
OptTypeInt OptionType = 3
|
OptTypeInt OptionType = 3
|
||||||
|
@ -24,6 +25,8 @@ const (
|
||||||
|
|
||||||
func getTypeName(t OptionType) string {
|
func getTypeName(t OptionType) string {
|
||||||
switch t {
|
switch t {
|
||||||
|
case optTypeAny:
|
||||||
|
return "any"
|
||||||
case OptTypeString:
|
case OptTypeString:
|
||||||
return "string"
|
return "string"
|
||||||
case OptTypeStringArray:
|
case OptTypeStringArray:
|
||||||
|
|
|
@ -75,7 +75,7 @@ func (p *Perspective) getPerspectiveValueCache(name string, requestedType Option
|
||||||
}
|
}
|
||||||
|
|
||||||
// check type
|
// check type
|
||||||
if requestedType != pOption.option.OptType {
|
if requestedType != pOption.option.OptType && requestedType != optTypeAny {
|
||||||
log.Errorf("config: bad type: requested %s as %s, but is %s", name, getTypeName(requestedType), getTypeName(pOption.option.OptType))
|
log.Errorf("config: bad type: requested %s as %s, but is %s", name, getTypeName(requestedType), getTypeName(pOption.option.OptType))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -88,6 +88,12 @@ func (p *Perspective) getPerspectiveValueCache(name string, requestedType Option
|
||||||
return pOption.valueCache
|
return pOption.valueCache
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Has returns whether the given option is set in the perspective.
|
||||||
|
func (p *Perspective) Has(name string) bool {
|
||||||
|
valueCache := p.getPerspectiveValueCache(name, optTypeAny)
|
||||||
|
return valueCache != nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetAsString returns a function that returns the wanted string with high performance.
|
// GetAsString returns a function that returns the wanted string with high performance.
|
||||||
func (p *Perspective) GetAsString(name string) (value string, ok bool) {
|
func (p *Perspective) GetAsString(name string) (value string, ok bool) {
|
||||||
valueCache := p.getPerspectiveValueCache(name, OptTypeString)
|
valueCache := p.getPerspectiveValueCache(name, OptTypeString)
|
||||||
|
|
Loading…
Add table
Reference in a new issue