mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-12 06:24:10 +01:00
chore: replace map[string]interface{} by gin.H
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
@@ -34,7 +35,7 @@ type ConfigRelatedOptions struct {
|
|||||||
// Parse parses the configuration file (if present) and the
|
// Parse parses the configuration file (if present) and the
|
||||||
// environment variables into the provided configuration.
|
// environment variables into the provided configuration.
|
||||||
func (c ConfigRelatedOptions) Parse(out io.Writer, component string, config interface{}) error {
|
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 cfgFile := c.Path; cfgFile != "" {
|
||||||
if strings.HasPrefix(cfgFile, "http://") || strings.HasPrefix(cfgFile, "https://") {
|
if strings.HasPrefix(cfgFile, "http://") || strings.HasPrefix(cfgFile, "https://") {
|
||||||
u, err := url.Parse(cfgFile)
|
u, err := url.Parse(cfgFile)
|
||||||
@@ -114,7 +115,7 @@ func (c ConfigRelatedOptions) Parse(out io.Writer, component string, config inte
|
|||||||
newRawConfig[index] = rawConfig
|
newRawConfig[index] = rawConfig
|
||||||
rawConfig = newRawConfig
|
rawConfig = newRawConfig
|
||||||
} else {
|
} else {
|
||||||
rawConfig = map[string]interface{}{
|
rawConfig = gin.H{
|
||||||
kk[i]: rawConfig,
|
kk[i]: rawConfig,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"akvorado/cmd"
|
"akvorado/cmd"
|
||||||
@@ -152,28 +153,28 @@ module2:
|
|||||||
t.Errorf("Parse() (-got, +want):\n%s", diff)
|
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 {
|
if err := yaml.Unmarshal(out.Bytes(), &gotRaw); err != nil {
|
||||||
t.Fatalf("Unmarshal() error:\n%+v", err)
|
t.Fatalf("Unmarshal() error:\n%+v", err)
|
||||||
}
|
}
|
||||||
expectedRaw := map[string]interface{}{
|
expectedRaw := gin.H{
|
||||||
"module1": map[string]interface{}{
|
"module1": gin.H{
|
||||||
"listen": "127.0.0.1:8080",
|
"listen": "127.0.0.1:8080",
|
||||||
"topic": "flows",
|
"topic": "flows",
|
||||||
"workers": 100,
|
"workers": 100,
|
||||||
},
|
},
|
||||||
"module2": map[string]interface{}{
|
"module2": gin.H{
|
||||||
"stuff": "bye",
|
"stuff": "bye",
|
||||||
"details": map[string]interface{}{
|
"details": gin.H{
|
||||||
"workers": 5,
|
"workers": 5,
|
||||||
"intervalvalue": "20m0s",
|
"intervalvalue": "20m0s",
|
||||||
},
|
},
|
||||||
"elements": []interface{}{
|
"elements": []interface{}{
|
||||||
map[string]interface{}{
|
gin.H{
|
||||||
"name": "first",
|
"name": "first",
|
||||||
"gauge": 67,
|
"gauge": 67,
|
||||||
},
|
},
|
||||||
map[string]interface{}{
|
gin.H{
|
||||||
"name": "second",
|
"name": "second",
|
||||||
"gauge": 0,
|
"gauge": 0,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/kentik/patricia"
|
"github.com/kentik/patricia"
|
||||||
tree "github.com/kentik/patricia/generics_tree"
|
tree "github.com/kentik/patricia/generics_tree"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
@@ -97,7 +98,7 @@ func SubnetMapUnmarshallerHook[V any]() mapstructure.DecodeHookFunc {
|
|||||||
if to.Type() != reflect.TypeOf(SubnetMap[V]{}) {
|
if to.Type() != reflect.TypeOf(SubnetMap[V]{}) {
|
||||||
return from.Interface(), nil
|
return from.Interface(), nil
|
||||||
}
|
}
|
||||||
output := map[string]interface{}{}
|
output := gin.H{}
|
||||||
var zero V
|
var zero V
|
||||||
var plausibleSubnetMap bool
|
var plausibleSubnetMap bool
|
||||||
if from.Kind() == reflect.Map {
|
if from.Kind() == reflect.Map {
|
||||||
|
|||||||
@@ -133,13 +133,13 @@ func TestHealthcheckHTTPHandler(t *testing.T) {
|
|||||||
|
|
||||||
reader := bufio.NewReader(w.Body)
|
reader := bufio.NewReader(w.Body)
|
||||||
decoder := json.NewDecoder(reader)
|
decoder := json.NewDecoder(reader)
|
||||||
var got map[string]interface{}
|
var got gin.H
|
||||||
if err := decoder.Decode(&got); err != nil {
|
if err := decoder.Decode(&got); err != nil {
|
||||||
t.Fatalf("GET /api/v0/healthcheck error:\n%+v", err)
|
t.Fatalf("GET /api/v0/healthcheck error:\n%+v", err)
|
||||||
}
|
}
|
||||||
expected := map[string]interface{}{
|
expected := gin.H{
|
||||||
"status": "error",
|
"status": "error",
|
||||||
"details": map[string]interface{}{
|
"details": gin.H{
|
||||||
"hc1": map[string]string{
|
"hc1": map[string]string{
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"reason": "all well",
|
"reason": "all well",
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
gormlogger "gorm.io/gorm/logger"
|
gormlogger "gorm.io/gorm/logger"
|
||||||
"gorm.io/gorm/utils"
|
"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) {
|
func (l *logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
|
||||||
elapsed := time.Since(begin)
|
elapsed := time.Since(begin)
|
||||||
sql, _ := fc()
|
sql, _ := fc()
|
||||||
fields := map[string]interface{}{
|
fields := gin.H{
|
||||||
"sql": sql,
|
"sql": sql,
|
||||||
"duration": elapsed,
|
"duration": elapsed,
|
||||||
"source": utils.FileWithLineNum(),
|
"source": utils.FileWithLineNum(),
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Shopify/sarama"
|
"github.com/Shopify/sarama"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
|
|
||||||
"akvorado/common/daemon"
|
"akvorado/common/daemon"
|
||||||
@@ -231,11 +232,11 @@ func TestCore(t *testing.T) {
|
|||||||
reader := bufio.NewReader(resp.Body)
|
reader := bufio.NewReader(resp.Body)
|
||||||
decoder := json.NewDecoder(reader)
|
decoder := json.NewDecoder(reader)
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
var got map[string]interface{}
|
var got gin.H
|
||||||
if err := decoder.Decode(&got); err != nil {
|
if err := decoder.Decode(&got); err != nil {
|
||||||
t.Fatalf("GET /api/v0/inlet/flows error while reading body:\n%+v", err)
|
t.Fatalf("GET /api/v0/inlet/flows error while reading body:\n%+v", err)
|
||||||
}
|
}
|
||||||
expected := map[string]interface{}{
|
expected := gin.H{
|
||||||
"TimeReceived": 200,
|
"TimeReceived": 200,
|
||||||
"SequenceNum": 1000,
|
"SequenceNum": 1000,
|
||||||
"SamplingRate": 1000,
|
"SamplingRate": 1000,
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
|
|
||||||
"akvorado/common/helpers"
|
"akvorado/common/helpers"
|
||||||
@@ -55,7 +56,7 @@ func ConfigurationUnmarshallerHook() mapstructure.DecodeHookFunc {
|
|||||||
return from.Interface(), nil
|
return from.Interface(), nil
|
||||||
}
|
}
|
||||||
configField := to.FieldByName("Config")
|
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
|
// Find "type" key in map to get input type. Keep
|
||||||
// "decoder" as is. Move everything else in "config".
|
// "decoder" as is. Move everything else in "config".
|
||||||
@@ -139,7 +140,7 @@ func (ic InputConfiguration) MarshalYAML() (interface{}, error) {
|
|||||||
if typeStr == "" {
|
if typeStr == "" {
|
||||||
return nil, errors.New("unable to guess input configuration type")
|
return nil, errors.New("unable to guess input configuration type")
|
||||||
}
|
}
|
||||||
result := map[string]interface{}{
|
result := gin.H{
|
||||||
"type": typeStr,
|
"type": typeStr,
|
||||||
"decoder": ic.Decoder,
|
"decoder": ic.Decoder,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
@@ -25,8 +26,8 @@ func TestDecodeConfiguration(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Name: "from empty configuration",
|
Name: "from empty configuration",
|
||||||
From: Configuration{},
|
From: Configuration{},
|
||||||
Source: map[string]interface{}{
|
Source: gin.H{
|
||||||
"inputs": []map[string]interface{}{
|
"inputs": []gin.H{
|
||||||
{
|
{
|
||||||
"type": "udp",
|
"type": "udp",
|
||||||
"decoder": "netflow",
|
"decoder": "netflow",
|
||||||
@@ -68,8 +69,8 @@ func TestDecodeConfiguration(t *testing.T) {
|
|||||||
Config: udp.DefaultConfiguration(),
|
Config: udp.DefaultConfiguration(),
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
Source: map[string]interface{}{
|
Source: gin.H{
|
||||||
"inputs": []map[string]interface{}{
|
"inputs": []gin.H{
|
||||||
{
|
{
|
||||||
"type": "udp",
|
"type": "udp",
|
||||||
"decoder": "netflow",
|
"decoder": "netflow",
|
||||||
@@ -108,8 +109,8 @@ func TestDecodeConfiguration(t *testing.T) {
|
|||||||
Config: udp.DefaultConfiguration(),
|
Config: udp.DefaultConfiguration(),
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
Source: map[string]interface{}{
|
Source: gin.H{
|
||||||
"inputs": []map[string]interface{}{
|
"inputs": []gin.H{
|
||||||
{
|
{
|
||||||
"type": "file",
|
"type": "file",
|
||||||
"paths": []string{"file1", "file2"},
|
"paths": []string{"file1", "file2"},
|
||||||
@@ -136,8 +137,8 @@ func TestDecodeConfiguration(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
Source: map[string]interface{}{
|
Source: gin.H{
|
||||||
"inputs": []map[string]interface{}{
|
"inputs": []gin.H{
|
||||||
{
|
{
|
||||||
"listen": "192.0.2.1:2055",
|
"listen": "192.0.2.1:2055",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
func TestNetworkNamesUnmarshalHook(t *testing.T) {
|
func TestNetworkNamesUnmarshalHook(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
Description string
|
Description string
|
||||||
Input map[string]interface{}
|
Input gin.H
|
||||||
Output map[string]NetworkAttributes
|
Output map[string]NetworkAttributes
|
||||||
Error bool
|
Error bool
|
||||||
}{
|
}{
|
||||||
|
|||||||
Reference in New Issue
Block a user