feat(Go-Tool):

2020/12/16: 添加YAML和结构体转换工具
This commit is contained in:
Huangzj
2020-12-16 14:23:38 +08:00
parent 61966551cd
commit dc80b557e3
32 changed files with 1700 additions and 324 deletions
+57
View File
@@ -0,0 +1,57 @@
/*
* @Author : huangzj
* @Time : 2020/12/16 11:56
* @Description
*/
package yaml
import (
"gopkg.in/yaml.v2"
"io/ioutil"
)
//把文件中的yaml结构转换成结构体
func GetYamlToStructByFile(fileName string, object interface{}) error {
yamlFile, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, object)
if err != nil {
return err
}
return nil
}
//通过字符串的Yaml转换成结构体
func GetYamlToStructByString(content string, object interface{}) error {
err := yaml.Unmarshal([]byte(content), object)
if err != nil {
return err
}
return nil
}
//通过字符串的Yaml转换成map
func GetYamlToMap(content string) (error, map[interface{}]interface{}) {
m := make(map[interface{}]interface{}, 0)
err := yaml.Unmarshal([]byte(content), m)
if err != nil {
return err, m
}
return nil, m
}
//把结构体转换成yaml的字符串
func GetStructToYaml(object interface{}) (error, string) {
result, err := yaml.Marshal(object)
if err != nil {
return err, ""
}
return nil, string(result)
}
+79
View File
@@ -0,0 +1,79 @@
/*
* @Author : huangzj
* @Time : 2020/12/16 11:56
* @Description
*/
package yaml
import (
"fmt"
"io/ioutil"
"testing"
)
type MySQLConfig struct {
User string `yaml:"user"`
Password string `yaml:"password"`
Host string `yaml:"host"`
Port int `yaml:"port"`
DbName string `yaml:"dbname"`
}
type RedisConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Auth string `yaml:"-"` //标签表示不显示
}
type NginxProxyConfig struct {
Counter int `yaml:"counter"`
NginxList []string `yaml:"nginx_list"`
}
type ServiceYaml struct {
MySQL MySQLConfig `yaml:"mysql"`
Redis RedisConfig `yaml:"redis"`
NginxProxy NginxProxyConfig `yaml:"nginx_proxy"`
}
func TestYamlToStructByFile(t *testing.T) {
config := new(ServiceYaml)
_ = GetYamlToStructByFile("config.yaml", &config)
fmt.Printf("MySQL host : %s, port : %d, user : %s, password : %s, db_name : %s \n",
config.MySQL.Host, config.MySQL.Port, config.MySQL.User, config.MySQL.Password, config.MySQL.DbName)
fmt.Printf("Redis host : %s, port %d, auth : %s\n",
config.Redis.Host, config.Redis.Port, config.Redis.Auth)
fmt.Printf("Vip Counter: %d, Vip List : %v\n", config.NginxProxy.Counter, config.NginxProxy.NginxList)
}
func TestYamlToStructByString(t *testing.T) {
config := new(ServiceYaml)
fileByte, _ := ioutil.ReadFile("config.yaml")
_ = GetYamlToStructByString(string(fileByte), &config)
fmt.Printf("MySQL host : %s, port : %d, user : %s, password : %s, db_name : %s \n",
config.MySQL.Host, config.MySQL.Port, config.MySQL.User, config.MySQL.Password, config.MySQL.DbName)
fmt.Printf("Redis host : %s, port %d, auth : %s\n",
config.Redis.Host, config.Redis.Port, config.Redis.Auth)
fmt.Printf("Vip Counter: %d, Vip List : %v\n", config.NginxProxy.Counter, config.NginxProxy.NginxList)
}
func TestYamlToMap(t *testing.T) {
fileByte, _ := ioutil.ReadFile("config.yaml")
_, m := GetYamlToMap(string(fileByte))
for key, value := range m {
fmt.Println(fmt.Sprintf("key:%v ,value: %v", key, value))
}
}
func TestStructToYaml(t *testing.T) {
config := new(ServiceYaml)
fileByte, _ := ioutil.ReadFile("config.yaml")
_ = GetYamlToStructByString(string(fileByte), &config)
_, result := GetStructToYaml(config)
fmt.Println(result)
}
+17
View File
@@ -0,0 +1,17 @@
mysql:
user: root
password: 123456
host: 192.198.1.1
port: 3306
dbname: mdb
redis:
host: 192.168.1.1
port: 1234
auth: 123456
nginx_proxy:
counter: 3
nginx_list: [
192.168.1.1,
192.168.1.2,
192.168.1.3
]
+5
View File
@@ -0,0 +1,5 @@
获取包命令: https://gopkg.in/yaml.v2
文档地址: https://gopkg.in/yaml.v2
参考地址:http://www.topgoer.com/%E5%85%B6%E4%BB%96/Yaml%E7%BC%96%E7%A0%81%E5%92%8C%E8%A7%A3%E7%A0%81.html