Эх сурвалжийг харах

增加了 测试案例
将 config 中的vier 对象暴露出来了

fly 1 жил өмнө
parent
commit
152898da1d
3 өөрчлөгдсөн 45 нэмэгдсэн , 26 устгасан
  1. 21 13
      config/config_test.go
  2. 23 12
      config/object.go
  3. 1 1
      go.mod

+ 21 - 13
config/config_test.go

@@ -1,6 +1,8 @@
 package config
 
 import (
+	"fmt"
+	"github.com/spf13/viper"
 	"testing"
 )
 
@@ -27,26 +29,32 @@ func TestSetConfigInfo(t *testing.T) {
 		config string
 		path   string
 	}
-	tests := []struct {
+	type testArgs struct {
 		name string
 		args args
-	}{
-		// TODO: Add test cases.
-		{name: "t", args: args{
-			name:   "test1",
-			config: "ini",
-			path:   ".",
-		}},
-		{name: "t1", args: args{
-			name:   "test2",
-			config: "yaml",
-			path:   "./test/",
-		}},
+	}
+	tests := make([]testArgs, 10)
+	for i, ext := range viper.SupportedExts {
+		test := testArgs{
+			name: fmt.Sprintf("t%d", i),
+			args: args{
+				name:   fmt.Sprintf("test%d", i),
+				config: ext,
+				path:   ".",
+			},
+		}
+		tests = append(tests, test)
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 			t.Log(tt)
 			SetConfigInfo(tt.args.name, tt.args.config, tt.args.path)
+			Register("http", "baseUrl", "http://192.168.0.141:8080")
+			Register("mqtt", "host", "http://192.168.0.141:8080")
+			Register("sensor", "uart.port", "/dev/ttyS2")
+			Register("sensor", "points", []byte{0, 1, 2, 3, 4, 5, 6})
+			Register("sensor", "key", 3)
+			Set("sensor", "test", 999)
 			Store()
 
 		})

+ 23 - 12
config/object.go

@@ -3,46 +3,57 @@ package config
 import "github.com/spf13/viper"
 
 type config struct {
-	viper *viper.Viper
+	Viper *viper.Viper
+}
+
+func New() *config {
+	var c config
+
+	c.Viper = viper.New()
+	c.Viper.SetConfigName(defaultName) //defaultName := lib.AppName()
+	c.Viper.SetConfigType(defaultConfigType)
+	c.Viper.AddConfigPath(defaultPath)
+
+	return &c
 }
 
 func NewConfig(name, suffix, path string) *config {
 	var c config
 
-	c.viper = viper.New()
-	c.viper.SetConfigName(name) //defaultName := lib.AppName()
-	c.viper.SetConfigType(suffix)
-	c.viper.AddConfigPath(path)
+	c.Viper = viper.New()
+	c.Viper.SetConfigName(name) //defaultName := lib.AppName()
+	c.Viper.SetConfigType(suffix)
+	c.Viper.AddConfigPath(path)
 
 	return &c
 }
 
 func (c *config) Load() error {
-	return c.viper.ReadInConfig()
+	return c.Viper.ReadInConfig()
 }
 
 func (c *config) Store() error {
-	return c.viper.SafeWriteConfig()
+	return c.Viper.SafeWriteConfig()
 }
 
 func (c *config) Register(module, key string, value any) {
-	c.viper.SetDefault(module+"."+key, value)
+	c.Viper.SetDefault(module+"."+key, value)
 }
 
 func (c *config) GetBool(module, key string) bool {
-	return c.viper.GetBool(module + "." + key)
+	return c.Viper.GetBool(module + "." + key)
 }
 
 func (c *config) GetString(module, key string) string {
-	return c.viper.GetString(module + "." + key)
+	return c.Viper.GetString(module + "." + key)
 }
 
 func (c *config) GetInt(module, key string) int {
-	return c.viper.GetInt(module + "." + key)
+	return c.Viper.GetInt(module + "." + key)
 }
 
 func (c *config) GetFloat(module, key string) float64 {
-	return c.viper.GetFloat64(module + "." + key)
+	return c.Viper.GetFloat64(module + "." + key)
 }
 
 func (c *config) GetStringSlice(module string, key string) []string {

+ 1 - 1
go.mod

@@ -1,6 +1,6 @@
 module git.familybaby.top/utils
 
-go 1.17
+go 1.18
 
 require (
 	github.com/eclipse/paho.mqtt.golang v1.4.3