config: minor changes to split config and watcher files
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
		
							
								
								
									
										34
									
								
								env.go
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								env.go
									
									
									
									
									
								
							| @@ -50,7 +50,7 @@ func (c *envConfig) Load(ctx context.Context, opts ...config.LoadOption) error { | ||||
|  | ||||
| 	src, err := rutil.Zero(c.opts.Struct) | ||||
| 	if err == nil { | ||||
| 		err = c.fillValues(ctx, reflect.ValueOf(src)) | ||||
| 		err = fillValues(ctx, reflect.ValueOf(src), c.opts.StructTag) | ||||
| 	} | ||||
| 	if err != nil && !c.opts.AllowFail { | ||||
| 		return err | ||||
| @@ -70,7 +70,7 @@ func (c *envConfig) Load(ctx context.Context, opts ...config.LoadOption) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (c *envConfig) fillValue(ctx context.Context, value reflect.Value, val string) error { | ||||
| func fillValue(ctx context.Context, value reflect.Value, val string) error { | ||||
| 	switch value.Kind() { | ||||
| 	case reflect.Map: | ||||
| 		t := value.Type() | ||||
| @@ -84,10 +84,10 @@ func (c *envConfig) fillValue(ctx context.Context, value reflect.Value, val stri | ||||
| 			kv := strings.FieldsFunc(nval, func(c rune) bool { return c == '=' }) | ||||
| 			mkey := reflect.Indirect(reflect.New(kt)) | ||||
| 			mval := reflect.Indirect(reflect.New(et)) | ||||
| 			if err := c.fillValue(ctx, mkey, kv[0]); err != nil { | ||||
| 			if err := fillValue(ctx, mkey, kv[0]); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			if err := c.fillValue(ctx, mval, kv[1]); err != nil { | ||||
| 			if err := fillValue(ctx, mval, kv[1]); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			value.SetMapIndex(mkey, mval) | ||||
| @@ -97,7 +97,7 @@ func (c *envConfig) fillValue(ctx context.Context, value reflect.Value, val stri | ||||
| 		value.Set(reflect.MakeSlice(reflect.SliceOf(value.Type().Elem()), len(nvals), len(nvals))) | ||||
| 		for idx, nval := range nvals { | ||||
| 			nvalue := reflect.Indirect(reflect.New(value.Type().Elem())) | ||||
| 			if err := c.fillValue(ctx, nvalue, nval); err != nil { | ||||
| 			if err := fillValue(ctx, nvalue, nval); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			value.Index(idx).Set(nvalue) | ||||
| @@ -237,7 +237,7 @@ func (c *envConfig) setValues(ctx context.Context, valueOf reflect.Value) error | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (c *envConfig) fillValues(ctx context.Context, valueOf reflect.Value) error { | ||||
| func fillValues(ctx context.Context, valueOf reflect.Value, structTag string) error { | ||||
| 	var values reflect.Value | ||||
|  | ||||
| 	if valueOf.Kind() == reflect.Ptr { | ||||
| @@ -264,7 +264,7 @@ func (c *envConfig) fillValues(ctx context.Context, valueOf reflect.Value) error | ||||
| 		switch value.Kind() { | ||||
| 		case reflect.Struct: | ||||
| 			value.Set(reflect.Indirect(reflect.New(value.Type()))) | ||||
| 			if err := c.fillValues(ctx, value); err != nil { | ||||
| 			if err := fillValues(ctx, value, structTag); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			continue | ||||
| @@ -278,12 +278,12 @@ func (c *envConfig) fillValues(ctx context.Context, valueOf reflect.Value) error | ||||
| 				value.Set(reflect.New(value.Type().Elem())) | ||||
| 			} | ||||
| 			value = value.Elem() | ||||
| 			if err := c.fillValues(ctx, value); err != nil { | ||||
| 			if err := fillValues(ctx, value, structTag); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			continue | ||||
| 		} | ||||
| 		tag, ok := field.Tag.Lookup(c.opts.StructTag) | ||||
| 		tag, ok := field.Tag.Lookup(structTag) | ||||
| 		if !ok { | ||||
| 			continue | ||||
| 		} | ||||
| @@ -292,7 +292,7 @@ func (c *envConfig) fillValues(ctx context.Context, valueOf reflect.Value) error | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		if err := c.fillValue(ctx, value, val); err != nil { | ||||
| 		if err := fillValue(ctx, value, val); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| @@ -328,6 +328,20 @@ func (c *envConfig) Name() string { | ||||
| 	return c.opts.Name | ||||
| } | ||||
|  | ||||
| func (c *envConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) { | ||||
| 	w := &envWatcher{ | ||||
| 		opts:  c.opts, | ||||
| 		wopts: config.NewWatchOptions(opts...), | ||||
| 		done:  make(chan struct{}), | ||||
| 		vchan: make(chan map[string]interface{}), | ||||
| 		echan: make(chan error), | ||||
| 	} | ||||
|  | ||||
| 	go w.run() | ||||
|  | ||||
| 	return w, nil | ||||
| } | ||||
|  | ||||
| func NewConfig(opts ...config.Option) config.Config { | ||||
| 	options := config.NewOptions(opts...) | ||||
| 	if len(options.StructTag) == 0 { | ||||
|   | ||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							| @@ -4,5 +4,5 @@ go 1.16 | ||||
|  | ||||
| require ( | ||||
| 	github.com/imdario/mergo v0.3.12 | ||||
| 	github.com/unistack-org/micro/v3 v3.4.0 | ||||
| 	github.com/unistack-org/micro/v3 v3.5.9 | ||||
| ) | ||||
|   | ||||
							
								
								
									
										8
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								go.sum
									
									
									
									
									
								
							| @@ -1,12 +1,12 @@ | ||||
| github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= | ||||
| github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg= | ||||
| github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||||
| github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= | ||||
| github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||||
| github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= | ||||
| github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= | ||||
| github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= | ||||
| github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= | ||||
| github.com/unistack-org/micro/v3 v3.4.0 h1:z9F3lgAb2j4cZ1ib5qBj66JPYUAzR4sNIJqUDjVwyVQ= | ||||
| github.com/unistack-org/micro/v3 v3.4.0/go.mod h1:LXmPfbJnJNvL0kQs8HfnkV3Wya2Wb+C7keVq++RCZnk= | ||||
| github.com/unistack-org/micro/v3 v3.5.9 h1:9iIxGZ56bVME7E9hqKIHeSHXkn69M9KFyJfaUzi7B9k= | ||||
| github.com/unistack-org/micro/v3 v3.5.9/go.mod h1:zQnZPEy842kQNcyjmVys6tdMjty4PHdyUUKYm1wrg1s= | ||||
| golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= | ||||
| golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||||
| golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||||
|   | ||||
							
								
								
									
										82
									
								
								watcher.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								watcher.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,82 @@ | ||||
| package env | ||||
|  | ||||
| import ( | ||||
| 	"reflect" | ||||
|  | ||||
| 	"github.com/unistack-org/micro/v3/config" | ||||
| 	"github.com/unistack-org/micro/v3/util/jitter" | ||||
| 	rutil "github.com/unistack-org/micro/v3/util/reflect" | ||||
| ) | ||||
|  | ||||
| type envWatcher struct { | ||||
| 	opts  config.Options | ||||
| 	wopts config.WatchOptions | ||||
| 	done  chan struct{} | ||||
| 	vchan chan map[string]interface{} | ||||
| 	echan chan error | ||||
| } | ||||
|  | ||||
| func (w *envWatcher) run() { | ||||
| 	ticker := jitter.NewTicker(w.wopts.MinInterval, w.wopts.MaxInterval) | ||||
| 	defer ticker.Stop() | ||||
|  | ||||
| 	src := w.opts.Struct | ||||
| 	if w.wopts.Struct != nil { | ||||
| 		src = w.wopts.Struct | ||||
| 	} | ||||
|  | ||||
| 	for { | ||||
| 		select { | ||||
| 		case <-w.done: | ||||
| 			return | ||||
| 		case <-ticker.C: | ||||
| 			dst, err := rutil.Zero(src) | ||||
| 			if err == nil { | ||||
| 				err = fillValues(w.opts.Context, reflect.ValueOf(dst), w.opts.StructTag) | ||||
| 			} | ||||
| 			if err != nil { | ||||
| 				w.echan <- err | ||||
| 				return | ||||
| 			} | ||||
| 			srcmp, err := rutil.StructFieldsMap(src) | ||||
| 			if err != nil { | ||||
| 				w.echan <- err | ||||
| 				return | ||||
| 			} | ||||
| 			dstmp, err := rutil.StructFieldsMap(dst) | ||||
| 			if err != nil { | ||||
| 				w.echan <- err | ||||
| 				return | ||||
| 			} | ||||
| 			for sk, sv := range srcmp { | ||||
| 				if reflect.DeepEqual(dstmp[sk], sv) { | ||||
| 					delete(dstmp, sk) | ||||
| 				} | ||||
| 			} | ||||
| 			if len(dstmp) > 0 { | ||||
| 				w.vchan <- dstmp | ||||
| 				src = dst | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (w *envWatcher) Next() (map[string]interface{}, error) { | ||||
| 	select { | ||||
| 	case <-w.done: | ||||
| 		break | ||||
| 	case err := <-w.echan: | ||||
| 		return nil, err | ||||
| 	case v, ok := <-w.vchan: | ||||
| 		if !ok { | ||||
| 			break | ||||
| 		} | ||||
| 		return v, nil | ||||
| 	} | ||||
| 	return nil, config.ErrWatcherStopped | ||||
| } | ||||
|  | ||||
| func (w *envWatcher) Stop() error { | ||||
| 	close(w.done) | ||||
| 	return nil | ||||
| } | ||||
		Reference in New Issue
	
	Block a user