Compare commits
83 Commits
Author | SHA1 | Date | |
---|---|---|---|
b579ffb5ea | |||
6b4202956e | |||
37e0380939 | |||
|
3cac13e35b | ||
d52e22c187 | |||
3cc1cf899a | |||
89b41364c3 | |||
d7ffcd19f8 | |||
6b27204711 | |||
4bee1a7041 | |||
f8f36b157f | |||
7018485fde | |||
dcae9f14ee | |||
510dd16e48 | |||
|
257964fb26 | ||
11fe3f3ef7 | |||
|
ba9aa37eae | ||
d54d71403c | |||
|
d9c127339d | ||
2290d7573b | |||
|
7f449da60b | ||
c01614781a | |||
|
95606b8f21 | ||
c70f6386d0 | |||
|
28c896b8fb | ||
|
0c0e1307bd | ||
|
1f82ecea93 | ||
76101e6ce1 | |||
|
b830dc9749 | ||
8988703465 | |||
|
0ec641747a | ||
fa03d3b557 | |||
|
2cebca91a6 | ||
5204de7cbb | |||
|
dcebdb62f2 | ||
5877222e8a | |||
|
811c83a189 | ||
0b610c430e | |||
|
e441398ab4 | ||
|
df29357782 | ||
dc0b42f998 | |||
|
c9657f0b20 | ||
150d1e57f6 | |||
|
2c31e3b350 | ||
|
7794d7a281 | ||
|
083ef551a0 | ||
|
0ecb1c1659 | ||
|
e49cae189a | ||
909ad599bb | |||
|
784b309405 | ||
9f71fdea5f | |||
|
dc84a2eb2a | ||
|
ad3e3e3247 | ||
|
006ed2929c | ||
3920f11b69 | |||
|
a84556fc6f | ||
|
6a936fa610 | ||
|
a358f95f2e | ||
23e0b7671b | |||
|
a60d877ed1 | ||
3728d5ed05 | |||
|
2ee7357913 | ||
4fbfd6da4d | |||
|
747def6210 | ||
f5fe1f7318 | |||
|
792d9734c2 | ||
697673296b | |||
|
58860cbb4c | ||
76722cd0de | |||
764e827a9e | |||
|
0f185974fe | ||
|
659f0a2446 | ||
|
345f9cc563 | ||
|
d1b85de9ec | ||
|
4fc4a78a77 | ||
|
f9828bcbf2 | ||
|
5810834afd | ||
|
ca1d9e8da5 | ||
|
bded061837 | ||
|
93c1ac7971 | ||
|
3cce97dc16 | ||
|
b09c5e52e3 | ||
2de3b60cd3 |
42
file.go
42
file.go
@ -1,4 +1,4 @@
|
|||||||
package file
|
package file // import "go.unistack.org/micro-config-file/v3"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@ -8,14 +8,15 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"dario.cat/mergo"
|
"dario.cat/mergo"
|
||||||
"go.unistack.org/micro/v4/codec"
|
"go.unistack.org/micro/v3/config"
|
||||||
"go.unistack.org/micro/v4/config"
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
||||||
"go.unistack.org/micro/v4/options"
|
|
||||||
rutil "go.unistack.org/micro/v4/util/reflect"
|
|
||||||
"golang.org/x/text/transform"
|
"golang.org/x/text/transform"
|
||||||
)
|
)
|
||||||
|
|
||||||
var DefaultStructTag = "file"
|
var (
|
||||||
|
DefaultStructTag = "file"
|
||||||
|
MaxFileSize int64 = 1 * 1024 * 1024
|
||||||
|
)
|
||||||
|
|
||||||
type fileConfig struct {
|
type fileConfig struct {
|
||||||
opts config.Options
|
opts config.Options
|
||||||
@ -28,17 +29,13 @@ func (c *fileConfig) Options() config.Options {
|
|||||||
return c.opts
|
return c.opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Init(opts ...options.Option) error {
|
func (c *fileConfig) Init(opts ...config.Option) error {
|
||||||
var err error
|
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||||
|
|
||||||
if err = config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
if err = o(&c.opts); err != nil {
|
o(&c.opts)
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.opts.Context != nil {
|
if c.opts.Context != nil {
|
||||||
@ -57,13 +54,6 @@ func (c *fileConfig) Init(opts ...options.Option) error {
|
|||||||
return fmt.Errorf("Codec must be specified")
|
return fmt.Errorf("Codec must be specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.path == "" && c.reader == nil {
|
|
||||||
err := fmt.Errorf("Path or Reader must be specified")
|
|
||||||
if !c.opts.AllowFail {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -71,7 +61,7 @@ func (c *fileConfig) Init(opts ...options.Option) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Load(ctx context.Context, opts ...options.Option) error {
|
func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
||||||
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
|
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -100,7 +90,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...options.Option) error {
|
|||||||
var fp io.Reader
|
var fp io.Reader
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if c.path != "" {
|
if path != "" {
|
||||||
fp, err = os.OpenFile(path, os.O_RDONLY, os.FileMode(0o400))
|
fp, err = os.OpenFile(path, os.O_RDONLY, os.FileMode(0o400))
|
||||||
} else if c.reader != nil {
|
} else if c.reader != nil {
|
||||||
fp = reader
|
fp = reader
|
||||||
@ -134,7 +124,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...options.Option) error {
|
|||||||
r = fp
|
r = fp
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, err := io.ReadAll(io.LimitReader(r, int64(codec.DefaultMaxMsgSize)))
|
buf, err := io.ReadAll(io.LimitReader(r, MaxFileSize))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !c.opts.AllowFail {
|
if !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
@ -178,7 +168,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...options.Option) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Save(ctx context.Context, opts ...options.Option) error {
|
func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
||||||
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
|
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -248,7 +238,7 @@ func (c *fileConfig) Name() string {
|
|||||||
return c.opts.Name
|
return c.opts.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Watch(ctx context.Context, opts ...options.Option) (config.Watcher, error) {
|
func (c *fileConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) {
|
||||||
path := c.path
|
path := c.path
|
||||||
options := config.NewWatchOptions(opts...)
|
options := config.NewWatchOptions(opts...)
|
||||||
if options.Context != nil {
|
if options.Context != nil {
|
||||||
@ -271,7 +261,7 @@ func (c *fileConfig) Watch(ctx context.Context, opts ...options.Option) (config.
|
|||||||
return w, nil
|
return w, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig(opts ...options.Option) config.Config {
|
func NewConfig(opts ...config.Option) config.Config {
|
||||||
options := config.NewOptions(opts...)
|
options := config.NewOptions(opts...)
|
||||||
if len(options.StructTag) == 0 {
|
if len(options.StructTag) == 0 {
|
||||||
options.StructTag = DefaultStructTag
|
options.StructTag = DefaultStructTag
|
||||||
|
20
file_test.go
20
file_test.go
@ -4,13 +4,11 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"go.unistack.org/micro/v4/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
"go.unistack.org/micro/v4/config"
|
"go.unistack.org/micro/v3/config"
|
||||||
"go.unistack.org/micro/v4/options"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type jsoncodec struct{}
|
type jsoncodec struct{}
|
||||||
@ -23,22 +21,10 @@ func (*jsoncodec) Unmarshal(buf []byte, v interface{}, opts ...codec.Option) err
|
|||||||
return json.Unmarshal(buf, v)
|
return json.Unmarshal(buf, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*jsoncodec) ReadBody(r io.Reader, v interface{}) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*jsoncodec) ReadHeader(r io.Reader, m *codec.Message, t codec.MessageType) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*jsoncodec) String() string {
|
func (*jsoncodec) String() string {
|
||||||
return "json"
|
return "json"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*jsoncodec) Write(w io.Writer, m *codec.Message, v interface{}) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLoadReplace(t *testing.T) {
|
func TestLoadReplace(t *testing.T) {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Key string
|
Key string
|
||||||
@ -52,7 +38,7 @@ func TestLoadReplace(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
c := NewConfig(options.Codec(
|
c := NewConfig(config.Codec(
|
||||||
&jsoncodec{}),
|
&jsoncodec{}),
|
||||||
config.Struct(cfg),
|
config.Struct(cfg),
|
||||||
Reader(buf),
|
Reader(buf),
|
||||||
|
22
go.mod
22
go.mod
@ -1,10 +1,22 @@
|
|||||||
module go.unistack.org/micro-config-file/v4
|
module go.unistack.org/micro-config-file/v3
|
||||||
|
|
||||||
go 1.20
|
go 1.22
|
||||||
|
|
||||||
|
toolchain go1.23.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
dario.cat/mergo v1.0.0
|
dario.cat/mergo v1.0.1
|
||||||
go.unistack.org/micro/v4 v4.0.17
|
go.unistack.org/micro/v3 v3.11.14
|
||||||
|
golang.org/x/text v0.21.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require github.com/google/uuid v1.6.0 // indirect
|
require (
|
||||||
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
github.com/kr/pretty v0.3.1 // indirect
|
||||||
|
github.com/rogpeppe/go-internal v1.11.0 // indirect
|
||||||
|
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
|
||||||
|
google.golang.org/protobuf v1.35.2 // indirect
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
||||||
|
30
go.sum
30
go.sum
@ -1,9 +1,31 @@
|
|||||||
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
|
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
|
||||||
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
|
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
|
||||||
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
go.unistack.org/micro/v4 v4.0.17 h1:mF7uM+J4ILdG+1fcwzKYCwDlxhdbF/e1WnGzKKLnIXc=
|
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||||
go.unistack.org/micro/v4 v4.0.17/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||||
|
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||||
|
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
|
||||||
|
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
|
||||||
|
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
||||||
|
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
||||||
|
go.unistack.org/micro/v3 v3.11.14 h1:3e9T30Ih9cvqZTCD8inG1qsBWRk4x5ZinWuTiDFM4CE=
|
||||||
|
go.unistack.org/micro/v3 v3.11.14/go.mod h1:k++F5Ej4LIy3XnOW/oj3P7B97wp2t9yLSlqtUzMpatM=
|
||||||
|
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||||
|
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||||
|
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
|
||||||
|
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
26
options.go
26
options.go
@ -5,28 +5,40 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"go.unistack.org/micro/v4/options"
|
"go.unistack.org/micro/v3/config"
|
||||||
"golang.org/x/text/transform"
|
"golang.org/x/text/transform"
|
||||||
)
|
)
|
||||||
|
|
||||||
type pathKey struct{}
|
type pathKey struct{}
|
||||||
|
|
||||||
func Path(path string) options.Option {
|
func Path(path string) config.Option {
|
||||||
return options.ContextOption(pathKey{}, path)
|
return config.SetOption(pathKey{}, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadPath(path string) config.LoadOption {
|
||||||
|
return config.SetLoadOption(pathKey{}, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SavePath(path string) config.SaveOption {
|
||||||
|
return config.SetSaveOption(pathKey{}, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WatchPath(path string) config.WatchOption {
|
||||||
|
return config.SetWatchOption(pathKey{}, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
type readerKey struct{}
|
type readerKey struct{}
|
||||||
|
|
||||||
func Reader(r io.Reader) options.Option {
|
func Reader(r io.Reader) config.Option {
|
||||||
return options.ContextOption(readerKey{}, r)
|
return config.SetOption(readerKey{}, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
type transformerKey struct{}
|
type transformerKey struct{}
|
||||||
|
|
||||||
type TransformerFunc func(src []byte, index []int) []byte
|
type TransformerFunc func(src []byte, index []int) []byte
|
||||||
|
|
||||||
func Transformer(t transform.Transformer) options.Option {
|
func Transformer(t transform.Transformer) config.Option {
|
||||||
return options.ContextOption(transformerKey{}, t)
|
return config.SetOption(transformerKey{}, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEnvTransformer(rs string, trimLeft, trimRight int) (*EnvTransformer, error) {
|
func NewEnvTransformer(rs string, trimLeft, trimRight int) (*EnvTransformer, error) {
|
||||||
|
@ -6,10 +6,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"go.unistack.org/micro/v4/codec"
|
"go.unistack.org/micro/v3/config"
|
||||||
"go.unistack.org/micro/v4/config"
|
"go.unistack.org/micro/v3/util/jitter"
|
||||||
"go.unistack.org/micro/v4/util/jitter"
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
||||||
rutil "go.unistack.org/micro/v4/util/reflect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type fileWatcher struct {
|
type fileWatcher struct {
|
||||||
@ -43,7 +42,7 @@ func (w *fileWatcher) run() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err = io.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
|
buf, err = io.ReadAll(io.LimitReader(fp, MaxFileSize))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = w.opts.Codec.Unmarshal(buf, dst)
|
err = w.opts.Codec.Unmarshal(buf, dst)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user