Skip to content

Commit b679545

Browse files
author
tietang
committed
add Unmarshal(obj interface{}, prefixes ...string)
1 parent 149c09c commit b679545

14 files changed

+731
-502
lines changed

example/ccs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/tietang/props/kvs"
66
)
77

8-
func main() {
8+
func main_css() {
99
//p, err := props.ReadPropertyFile("config.properties")
1010
//if err != nil {
1111
// panic(err)

example/x.go

+7-18
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,13 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/tietang/props/kvs"
6-
"path"
7-
"strings"
5+
"reflect"
6+
"time"
87
)
98

10-
func main_1() {
11-
var ctype kvs.ContentType
12-
13-
k := "configs/dev/resk/mysql."
14-
key := path.Base(k)
15-
idx := strings.LastIndex(key, ".")
16-
fmt.Println(idx)
17-
if idx == -1 || idx == len(key)-1 {
18-
ctype = kvs.ContentProps
19-
} else {
20-
ctype = kvs.ContentType(key[idx+1:])
21-
}
22-
23-
fmt.Println(ctype)
24-
9+
func main() {
10+
s := time.Second * 10
11+
v := reflect.ValueOf(s)
12+
fmt.Println(v.Kind())
13+
fmt.Println(v.Kind())
2514
}

go.mod

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
module github.com/tietang/props
1+
module github.com/tietang/props/v3
22

33
go 1.12
44

55
//被墙的原因,替换golang.org源为github.com源
66
replace (
77
cloud.google.com/go => github.com/googleapis/google-cloud-go v0.37.2
8+
github.com/hashicorp/consul => github.com/hashicorp/consul v1.7.3
89
golang.org/x/build => github.com/golang/build v0.0.0-20190327004547-5a2224f3eb52
910
golang.org/x/crypto => github.com/golang/crypto v0.0.0-20190325154230-a5d413f7728c
1011
golang.org/x/exp => github.com/golang/exp v0.0.0-20190321205749-f0864edee7f3
@@ -38,41 +39,39 @@ require (
3839
github.com/gogo/protobuf v1.2.1 // indirect
3940
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect
4041
github.com/google/btree v1.0.0 // indirect
41-
github.com/gopherjs/gopherjs v0.0.0-20180628210949-0892b62f0d9f // indirect
4242
github.com/gorilla/websocket v1.4.0 // indirect
4343
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 // indirect
4444
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
4545
github.com/grpc-ecosystem/grpc-gateway v1.9.0 // indirect
4646
github.com/hashicorp/consul/api v1.4.0
47-
github.com/hashicorp/go-hclog v0.14.1 // indirect
4847
github.com/hashicorp/go-immutable-radix v1.2.0 // indirect
48+
github.com/hashicorp/go-msgpack v0.5.5 // indirect
49+
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
4950
github.com/hashicorp/golang-lru v0.5.4 // indirect
5051
github.com/hashicorp/serf v0.9.2 // indirect
51-
github.com/jonboulle/clockwork v0.1.0 // indirect
52-
github.com/jtolds/gls v4.20.0+incompatible // indirect
52+
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
5353
github.com/mitchellh/mapstructure v1.3.2 // indirect
54-
github.com/pascaldekloe/goe v0.1.0 // indirect
54+
github.com/nacos-group/nacos-sdk-go v0.3.2
5555
github.com/prometheus/common v0.10.0
5656
github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec
57+
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b
5758
github.com/sirupsen/logrus v1.6.0
58-
github.com/smartystreets/assertions v0.0.0-20180301161246-7678a5452ebe // indirect
59-
github.com/smartystreets/goconvey v0.0.0-20170602164621-9e8dc3f972df
60-
github.com/smartystreets/gunit v0.0.0-20180314194857-6f0d6275bdcd // indirect
59+
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a
6160
github.com/soheilhy/cmux v0.1.4 // indirect
6261
github.com/tietang/go-utils v0.1.3
62+
github.com/tietang/props v2.2.0+incompatible
6363
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 // indirect
6464
github.com/valyala/fasttemplate v1.1.0
6565
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
6666
go.etcd.io/bbolt v1.3.2 // indirect
6767
go.uber.org/atomic v1.4.0 // indirect
6868
go.uber.org/multierr v1.1.0 // indirect
6969
go.uber.org/zap v1.10.0 // indirect
70+
golang.org/x/crypto v0.0.0-20191106202628-ed6320f186d4 // indirect
7071
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
71-
golang.org/x/text v0.3.2 // indirect
7272
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
7373
google.golang.org/genproto v0.0.0-20190605220351-eb0b1bdb6ae6 // indirect
74-
google.golang.org/grpc v1.21.1 // indirect
75-
gopkg.in/ini.v1 v1.42.0 // indirect
74+
google.golang.org/grpc v1.23.0 // indirect
7675
gopkg.in/yaml.v2 v2.2.8
7776
)
7877

go.sum

-393
This file was deleted.

kvs/config_composite.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ func (ccs *CompositeConfigSource) SetAll(values map[string]string) {
300300
ccs.Properties.SetAll(values)
301301
}
302302

303-
func (ccs *CompositeConfigSource) Unmarshal(obj interface{}) error {
304-
return Unmarshal(ccs, obj)
303+
func (ccs *CompositeConfigSource) Unmarshal(obj interface{}, prefixes ...string) error {
304+
return Unmarshal(ccs, obj, prefixes...)
305305
}
306306

307307
func (ccs *CompositeConfigSource) Keys() []string {

kvs/config_source.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,5 @@ type ConfigSource interface {
104104
Keys() []string
105105
//KeysFilter(filter string) []string
106106
//t必须为指针型
107-
Unmarshal(t interface{}) error
107+
Unmarshal(obj interface{}, prefixes ...string) error
108108
}

kvs/map_props.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,15 @@ func (p *MapProperties) Clear() {
172172
p.Values = make(map[string]string)
173173
}
174174

175-
func (p *MapProperties) Unmarshal(obj interface{}) error {
176-
return Unmarshal(p, obj)
175+
func (p *MapProperties) Unmarshal(obj interface{}, prefixes ...string) error {
176+
return Unmarshal(p, obj, prefixes...)
177177
}
178178

179179
func Unmarshal(p ConfigSource, obj interface{}, parentKeys ...string) error {
180180
v := reflect.ValueOf(obj).Elem()
181181
return unmarshalInner(p, v, parentKeys...)
182182
}
183+
183184
func unmarshalInner(p ConfigSource, v reflect.Value, parentKeys ...string) error {
184185

185186
//t := reflect.TypeOf(obj)
@@ -350,7 +351,6 @@ func unmarshalInner(p ConfigSource, v reflect.Value, parentKeys ...string) error
350351
}
351352
}
352353
break
353-
break
354354
case reflect.Struct:
355355
//fmt.Println("---")
356356
unmarshalInner(p, value, keys...)

nacos/nacos_client_props_source.go

+191
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package nacos
2+
3+
import (
4+
"bytes"
5+
"github.com/nacos-group/nacos-sdk-go/clients"
6+
"github.com/nacos-group/nacos-sdk-go/clients/config_client"
7+
"github.com/nacos-group/nacos-sdk-go/common/constant"
8+
"github.com/nacos-group/nacos-sdk-go/vo"
9+
log "github.com/sirupsen/logrus"
10+
"github.com/tietang/props/kvs"
11+
"strconv"
12+
"strings"
13+
)
14+
15+
var _ kvs.ConfigSource = new(NacosClientPropsConfigSource)
16+
17+
//通过key/value来组织,过滤root prefix后,替换/为.作为properties key
18+
type NacosClientPropsConfigSource struct {
19+
kvs.MapProperties
20+
// Required Configuration ID. Use a naming rule similar to package.class (for example, com.taobao.tc.refund.log.level) to ensure global uniqueness. It is recommended to indicate business meaning of the configuration in the "class" section. Use lower case for all characters. Use alphabetical letters and these four special characters (".", ":", "-", "_") only. Up to 256 characters are allowed.
21+
DataId string
22+
// Required Configuration group. To ensure uniqueness, format such as product name: module name (for example, Nacos:Test) is preferred. Use alphabetical letters and these four special characters (".", ":", "-", "_") only. Up to 128 characters are allowed.
23+
Group string
24+
25+
//Tenant information. It corresponds to the Namespace field in Nacos.
26+
Tenant string
27+
NamespaceId string
28+
ContentType string
29+
AppName string
30+
31+
LineSeparator string
32+
KVSeparator string
33+
//
34+
name string
35+
lastCt uint32
36+
ClientConfig constant.ClientConfig
37+
ServerConfigs []constant.ServerConfig
38+
Client config_client.IConfigClient
39+
}
40+
41+
func NewNacosClientPropsConfigSource(address, group, dataId, tenant string) *NacosClientPropsConfigSource {
42+
s := &NacosClientPropsConfigSource{}
43+
name := strings.Join([]string{"Nacos", address}, ":")
44+
s.name = name
45+
s.DataId = dataId
46+
s.Group = group
47+
s.Tenant = tenant
48+
s.ClientConfig = constant.ClientConfig{
49+
TimeoutMs: 10 * 1000, //http请求超时时间,单位毫秒
50+
ListenInterval: 30 * 1000, //监听间隔时间,单位毫秒(仅在ConfigClient中有效)
51+
BeatInterval: 5 * 1000, //心跳间隔时间,单位毫秒(仅在ServiceClient中有效)
52+
CacheDir: "./data/nacos/cache", //缓存目录
53+
LogDir: "./data/nacos/log", //日志目录
54+
UpdateThreadNum: 20, //更新服务的线程数
55+
NotLoadCacheAtStart: true, //在启动时不读取本地缓存数据,true--不读取,false--读取
56+
UpdateCacheWhenEmpty: true, //当服务列表为空时是否更新本地缓存,true--更新,false--不更新
57+
}
58+
if len(tenant) > 0 {
59+
s.ClientConfig.NamespaceId = tenant
60+
s.NamespaceId = tenant
61+
}
62+
s.ServerConfigs = make([]constant.ServerConfig, 0)
63+
addrs := strings.Split(address, ",")
64+
for _, addr := range addrs {
65+
a := strings.Split(addr, ":")
66+
port := 80
67+
if len(a) == 2 {
68+
var err error
69+
port, err = strconv.Atoi(a[1])
70+
if err != nil {
71+
log.Error("error config nacos address:", addr)
72+
continue
73+
}
74+
}
75+
76+
s.ServerConfigs = append(s.ServerConfigs, constant.ServerConfig{
77+
IpAddr: a[0],
78+
ContextPath: "/nacos",
79+
Port: uint64(port),
80+
})
81+
}
82+
83+
s.Values = make(map[string]string)
84+
85+
var err error
86+
s.Client, err = clients.CreateConfigClient(map[string]interface{}{
87+
constant.KEY_SERVER_CONFIGS: s.ServerConfigs,
88+
constant.KEY_CLIENT_CONFIG: s.ClientConfig,
89+
})
90+
if err != nil {
91+
log.Panic("error create ConfigClient: ", err)
92+
}
93+
s.init()
94+
95+
return s
96+
}
97+
98+
func NewNacosClientPropsCompositeConfigSource(address, group, tenant string, dataIds []string) *kvs.CompositeConfigSource {
99+
s := kvs.NewEmptyNoSystemEnvCompositeConfigSource()
100+
s.ConfName = "NacosKevValue"
101+
for _, dataId := range dataIds {
102+
c := NewNacosClientPropsConfigSource(address, group, dataId, tenant)
103+
s.Add(c)
104+
}
105+
106+
return s
107+
}
108+
109+
func (s *NacosClientPropsConfigSource) init() {
110+
s.findProperties()
111+
s.watchContext()
112+
}
113+
114+
func (s *NacosClientPropsConfigSource) watchContext() {
115+
cp := vo.ConfigParam{
116+
DataId: "dataId",
117+
Group: "group",
118+
}
119+
if len(s.AppName) > 0 {
120+
cp.AppName = s.AppName
121+
}
122+
cp.OnChange = func(namespace, group, dataId, data string) {
123+
s.parseAndregisterProps([]byte(data))
124+
log.Info("changed config:", namespace, group, dataId)
125+
}
126+
s.Client.ListenConfig(cp)
127+
128+
}
129+
130+
func (s *NacosClientPropsConfigSource) Close() {
131+
}
132+
133+
func (s *NacosClientPropsConfigSource) findProperties() {
134+
135+
data, err := s.get()
136+
if err != nil {
137+
log.Error(err)
138+
return
139+
}
140+
s.parseAndregisterProps(data)
141+
142+
}
143+
144+
func (s *NacosClientPropsConfigSource) parseAndregisterProps(data []byte) {
145+
sep := s.LineSeparator
146+
if sep == "" {
147+
sep = NACOS_LINE_SEPARATOR
148+
}
149+
kvsep := s.KVSeparator
150+
if kvsep == "" {
151+
kvsep = NACOS_KV_SEPARATOR
152+
}
153+
lines := bytes.Split(data, []byte(sep))
154+
155+
for _, l := range lines {
156+
157+
i := bytes.Index(l, []byte(kvsep))
158+
if i <= 0 {
159+
continue
160+
}
161+
key := string(l[:i])
162+
value := string(l[i+1:])
163+
s.registerProps(key, value)
164+
//log.Info(key,"=",value)
165+
}
166+
}
167+
168+
func (s *NacosClientPropsConfigSource) registerProps(key, value string) {
169+
s.Set(strings.TrimSpace(key), strings.TrimSpace(value))
170+
171+
}
172+
173+
func (s *NacosClientPropsConfigSource) Name() string {
174+
return s.name
175+
}
176+
177+
func (h *NacosClientPropsConfigSource) get() (body []byte, err error) {
178+
cp := vo.ConfigParam{
179+
DataId: "dataId",
180+
Group: "group",
181+
}
182+
if len(h.AppName) > 0 {
183+
cp.AppName = h.AppName
184+
}
185+
content, err := h.Client.GetConfig(cp)
186+
if err != nil {
187+
log.Error(err)
188+
return nil, err
189+
}
190+
return []byte(content), err
191+
}

0 commit comments

Comments
 (0)