73 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			73 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Go
		
	
	
	
package utils
 | 
						||
 | 
						||
import (
 | 
						||
	"fmt"
 | 
						||
	"github.com/go-kratos/kratos/v2/log"
 | 
						||
	"google.golang.org/protobuf/runtime/protoimpl"
 | 
						||
	"gopkg.in/yaml.v3"
 | 
						||
	"io/fs"
 | 
						||
	"os"
 | 
						||
	"path/filepath"
 | 
						||
	"testing"
 | 
						||
	baseconf "trans_hub/base_conf"
 | 
						||
	"trans_hub/pkg"
 | 
						||
	"trans_hub/pkg/mapstructure"
 | 
						||
)
 | 
						||
 | 
						||
const SPACE = "public"
 | 
						||
const PORT = 8848
 | 
						||
const User = ""
 | 
						||
const Pass = ""
 | 
						||
const IP = "192.168.110.93"
 | 
						||
const Group = "DEFAULT_GROUP"
 | 
						||
const DataId = "PG_BASE_CONFIG"
 | 
						||
 | 
						||
func TestConfig(t *testing.T) {
 | 
						||
	type Nacos struct {
 | 
						||
		state         protoimpl.MessageState
 | 
						||
		sizeCache     protoimpl.SizeCache
 | 
						||
		unknownFields protoimpl.UnknownFields
 | 
						||
		Ip            string `protobuf:"bytes,1,opt,name=ip,proto3" json:"ip,omitempty"`
 | 
						||
		Port          uint64 `protobuf:"varint,2,opt,name=port,proto3" json:"port,omitempty"`
 | 
						||
	}
 | 
						||
	type Conf struct {
 | 
						||
		Nacos *Nacos `protobuf:"bytes,8,opt,name=nacos,proto3" json:"nacos,omitempty"`
 | 
						||
	}
 | 
						||
	var c Conf
 | 
						||
	nc := &baseconf.Nacos{Ip: IP, Port: PORT, Space: SPACE, User: User, Password: Pass}
 | 
						||
 | 
						||
	var s = ServerConfig(nc, Group, DataId)
 | 
						||
	err := mapstructure.Decode(s, &c)
 | 
						||
	t.Log(s, err)
 | 
						||
}
 | 
						||
 | 
						||
func TestMod(t *testing.T) {
 | 
						||
	dir := pkg.GetRootPath()
 | 
						||
	// 读取目录内容
 | 
						||
	err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
 | 
						||
		if err != nil {
 | 
						||
			return err
 | 
						||
		}
 | 
						||
		if !d.IsDir() && filepath.Ext(path) == ".yaml" {
 | 
						||
			data, err := os.ReadFile(path)
 | 
						||
			if err != nil {
 | 
						||
				return err
 | 
						||
			}
 | 
						||
			var result map[string]interface{}
 | 
						||
			err = yaml.Unmarshal(data, &result) // 解析YAML到map中,使用gopkg.v3的yaml包或其他你选择的版本(例如encoding/yaml)
 | 
						||
			if err != nil {
 | 
						||
				return err
 | 
						||
			}
 | 
						||
			fmt.Printf("File: %s\nContent: %+v\n", path, result)
 | 
						||
		}
 | 
						||
		return nil
 | 
						||
	})
 | 
						||
	if err != nil {
 | 
						||
		log.Fatal(err)
 | 
						||
	}
 | 
						||
}
 | 
						||
 | 
						||
func TestYaml(t *testing.T) {
 | 
						||
	t.Log(GetBaseYaml())
 | 
						||
}
 |