mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-12 06:24:10 +01:00
This should be generalized to other parts of configuration (SNMP community), however, we need to check what happens with default values. Also, network definition in orchestrator is likely to be able to reuse that.
45 lines
1.5 KiB
Go
45 lines
1.5 KiB
Go
// SPDX-FileCopyrightText: 2022 Free Mobile
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
package helpers
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
)
|
|
|
|
var mapstructureUnmarshallerHookFuncs = []mapstructure.DecodeHookFunc{}
|
|
|
|
// AddMapstructureUnmarshallerHook registers a new decoder hook for
|
|
// mapstructure. This should only be done during init.
|
|
func AddMapstructureUnmarshallerHook(hook mapstructure.DecodeHookFunc) {
|
|
mapstructureUnmarshallerHookFuncs = append(mapstructureUnmarshallerHookFuncs, hook)
|
|
}
|
|
|
|
// GetMapStructureDecoderConfig returns a decoder config for
|
|
// mapstructure with all registered hooks as well as appropriate
|
|
// default configuration.
|
|
func GetMapStructureDecoderConfig(config interface{}, hooks ...mapstructure.DecodeHookFunc) *mapstructure.DecoderConfig {
|
|
return &mapstructure.DecoderConfig{
|
|
Result: config,
|
|
ErrorUnused: true,
|
|
WeaklyTypedInput: true,
|
|
MatchName: MapStructureMatchName,
|
|
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
|
mapstructure.ComposeDecodeHookFunc(hooks...),
|
|
mapstructure.ComposeDecodeHookFunc(mapstructureUnmarshallerHookFuncs...),
|
|
mapstructure.TextUnmarshallerHookFunc(),
|
|
mapstructure.StringToTimeDurationHookFunc(),
|
|
mapstructure.StringToSliceHookFunc(","),
|
|
),
|
|
}
|
|
}
|
|
|
|
// MapStructureMatchName tells if map key and field names are equal.
|
|
func MapStructureMatchName(mapKey, fieldName string) bool {
|
|
key := strings.ToLower(strings.ReplaceAll(mapKey, "-", ""))
|
|
field := strings.ToLower(fieldName)
|
|
return key == field
|
|
}
|