chore: replace map[string]interface{} by gin.H

This commit is contained in:
Vincent Bernat
2022-08-16 18:49:54 +02:00
parent c8fac33990
commit 985e678e42
9 changed files with 34 additions and 27 deletions

View File

@@ -16,6 +16,7 @@ import (
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
@@ -34,7 +35,7 @@ type ConfigRelatedOptions struct {
// Parse parses the configuration file (if present) and the
// environment variables into the provided configuration.
func (c ConfigRelatedOptions) Parse(out io.Writer, component string, config interface{}) error {
var rawConfig map[string]interface{}
var rawConfig gin.H
if cfgFile := c.Path; cfgFile != "" {
if strings.HasPrefix(cfgFile, "http://") || strings.HasPrefix(cfgFile, "https://") {
u, err := url.Parse(cfgFile)
@@ -114,7 +115,7 @@ func (c ConfigRelatedOptions) Parse(out io.Writer, component string, config inte
newRawConfig[index] = rawConfig
rawConfig = newRawConfig
} else {
rawConfig = map[string]interface{}{
rawConfig = gin.H{
kk[i]: rawConfig,
}
}

View File

@@ -15,6 +15,7 @@ import (
"testing"
"time"
"github.com/gin-gonic/gin"
"gopkg.in/yaml.v2"
"akvorado/cmd"
@@ -152,28 +153,28 @@ module2:
t.Errorf("Parse() (-got, +want):\n%s", diff)
}
var gotRaw map[string]map[string]interface{}
var gotRaw map[string]gin.H
if err := yaml.Unmarshal(out.Bytes(), &gotRaw); err != nil {
t.Fatalf("Unmarshal() error:\n%+v", err)
}
expectedRaw := map[string]interface{}{
"module1": map[string]interface{}{
expectedRaw := gin.H{
"module1": gin.H{
"listen": "127.0.0.1:8080",
"topic": "flows",
"workers": 100,
},
"module2": map[string]interface{}{
"module2": gin.H{
"stuff": "bye",
"details": map[string]interface{}{
"details": gin.H{
"workers": 5,
"intervalvalue": "20m0s",
},
"elements": []interface{}{
map[string]interface{}{
gin.H{
"name": "first",
"gauge": 67,
},
map[string]interface{}{
gin.H{
"name": "second",
"gauge": 0,
},

View File

@@ -10,6 +10,7 @@ import (
"regexp"
"strings"
"github.com/gin-gonic/gin"
"github.com/kentik/patricia"
tree "github.com/kentik/patricia/generics_tree"
"github.com/mitchellh/mapstructure"
@@ -97,7 +98,7 @@ func SubnetMapUnmarshallerHook[V any]() mapstructure.DecodeHookFunc {
if to.Type() != reflect.TypeOf(SubnetMap[V]{}) {
return from.Interface(), nil
}
output := map[string]interface{}{}
output := gin.H{}
var zero V
var plausibleSubnetMap bool
if from.Kind() == reflect.Map {

View File

@@ -133,13 +133,13 @@ func TestHealthcheckHTTPHandler(t *testing.T) {
reader := bufio.NewReader(w.Body)
decoder := json.NewDecoder(reader)
var got map[string]interface{}
var got gin.H
if err := decoder.Decode(&got); err != nil {
t.Fatalf("GET /api/v0/healthcheck error:\n%+v", err)
}
expected := map[string]interface{}{
expected := gin.H{
"status": "error",
"details": map[string]interface{}{
"details": gin.H{
"hc1": map[string]string{
"status": "ok",
"reason": "all well",

View File

@@ -7,6 +7,7 @@ import (
"context"
"time"
"github.com/gin-gonic/gin"
gormlogger "gorm.io/gorm/logger"
"gorm.io/gorm/utils"
@@ -36,7 +37,7 @@ func (l *logger) Error(ctx context.Context, s string, args ...interface{}) {
func (l *logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
elapsed := time.Since(begin)
sql, _ := fc()
fields := map[string]interface{}{
fields := gin.H{
"sql": sql,
"duration": elapsed,
"source": utils.FileWithLineNum(),

View File

@@ -15,6 +15,7 @@ import (
"time"
"github.com/Shopify/sarama"
"github.com/gin-gonic/gin"
"github.com/golang/protobuf/proto"
"akvorado/common/daemon"
@@ -231,11 +232,11 @@ func TestCore(t *testing.T) {
reader := bufio.NewReader(resp.Body)
decoder := json.NewDecoder(reader)
for i := 0; i < 10; i++ {
var got map[string]interface{}
var got gin.H
if err := decoder.Decode(&got); err != nil {
t.Fatalf("GET /api/v0/inlet/flows error while reading body:\n%+v", err)
}
expected := map[string]interface{}{
expected := gin.H{
"TimeReceived": 200,
"SequenceNum": 1000,
"SamplingRate": 1000,

View File

@@ -10,6 +10,7 @@ import (
"reflect"
"strings"
"github.com/gin-gonic/gin"
"github.com/mitchellh/mapstructure"
"akvorado/common/helpers"
@@ -55,7 +56,7 @@ func ConfigurationUnmarshallerHook() mapstructure.DecodeHookFunc {
return from.Interface(), nil
}
configField := to.FieldByName("Config")
fromConfig := reflect.MakeMap(reflect.TypeOf(map[string]interface{}{}))
fromConfig := reflect.MakeMap(reflect.TypeOf(gin.H{}))
// Find "type" key in map to get input type. Keep
// "decoder" as is. Move everything else in "config".
@@ -139,7 +140,7 @@ func (ic InputConfiguration) MarshalYAML() (interface{}, error) {
if typeStr == "" {
return nil, errors.New("unable to guess input configuration type")
}
result := map[string]interface{}{
result := gin.H{
"type": typeStr,
"decoder": ic.Decoder,
}

View File

@@ -7,6 +7,7 @@ import (
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
@@ -25,8 +26,8 @@ func TestDecodeConfiguration(t *testing.T) {
{
Name: "from empty configuration",
From: Configuration{},
Source: map[string]interface{}{
"inputs": []map[string]interface{}{
Source: gin.H{
"inputs": []gin.H{
{
"type": "udp",
"decoder": "netflow",
@@ -68,8 +69,8 @@ func TestDecodeConfiguration(t *testing.T) {
Config: udp.DefaultConfiguration(),
}},
},
Source: map[string]interface{}{
"inputs": []map[string]interface{}{
Source: gin.H{
"inputs": []gin.H{
{
"type": "udp",
"decoder": "netflow",
@@ -108,8 +109,8 @@ func TestDecodeConfiguration(t *testing.T) {
Config: udp.DefaultConfiguration(),
}},
},
Source: map[string]interface{}{
"inputs": []map[string]interface{}{
Source: gin.H{
"inputs": []gin.H{
{
"type": "file",
"paths": []string{"file1", "file2"},
@@ -136,8 +137,8 @@ func TestDecodeConfiguration(t *testing.T) {
},
}},
},
Source: map[string]interface{}{
"inputs": []map[string]interface{}{
Source: gin.H{
"inputs": []gin.H{
{
"listen": "192.0.2.1:2055",
},

View File

@@ -15,7 +15,7 @@ import (
func TestNetworkNamesUnmarshalHook(t *testing.T) {
cases := []struct {
Description string
Input map[string]interface{}
Input gin.H
Output map[string]NetworkAttributes
Error bool
}{