Compare commits
37 Commits
Author | SHA1 | Date | |
---|---|---|---|
4377fb37da | |||
d17ad2df98 | |||
b77742f761 | |||
|
cefd3e0b25 | ||
ae787511f4 | |||
8368768957 | |||
2fa6be8f5a | |||
d77095fb4c | |||
4dc1cb3e1c | |||
38851a57bf | |||
1d81cb3c80 | |||
6061155190 | |||
632868864e | |||
a23c45515a | |||
5f74e48ef4 | |||
|
19cb6cf800 | ||
|
c0e0529819 | ||
3f6f91808b | |||
|
efd880b2ea | ||
69dec86aaa | |||
|
f1f442339d | ||
89d4f83671 | |||
|
9a608532a8 | ||
ef412b165f | |||
|
3c7e3ed5b8 | ||
8d35254abe | |||
|
c78878a918 | ||
443bc44da1 | |||
|
9ec934cc15 | ||
ae6cde7614 | |||
|
ba9fa173de | ||
5e4627d676 | |||
|
bc31a3f7db | ||
58a48621f1 | |||
|
8af739f80f | ||
2e91975c6b | |||
|
d0a93944e5 |
42
file.go
42
file.go
@ -1,4 +1,4 @@
|
||||
package file // import "go.unistack.org/micro-config-file/v3"
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
@ -8,15 +8,14 @@ import (
|
||||
"regexp"
|
||||
|
||||
"dario.cat/mergo"
|
||||
"go.unistack.org/micro/v3/config"
|
||||
rutil "go.unistack.org/micro/v3/util/reflect"
|
||||
"go.unistack.org/micro/v4/codec"
|
||||
"go.unistack.org/micro/v4/config"
|
||||
"go.unistack.org/micro/v4/options"
|
||||
rutil "go.unistack.org/micro/v4/util/reflect"
|
||||
"golang.org/x/text/transform"
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultStructTag = "file"
|
||||
MaxFileSize int64 = 1 * 1024 * 1024
|
||||
)
|
||||
var DefaultStructTag = "file"
|
||||
|
||||
type fileConfig struct {
|
||||
opts config.Options
|
||||
@ -29,13 +28,17 @@ func (c *fileConfig) Options() config.Options {
|
||||
return c.opts
|
||||
}
|
||||
|
||||
func (c *fileConfig) Init(opts ...config.Option) error {
|
||||
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||
func (c *fileConfig) Init(opts ...options.Option) error {
|
||||
var err error
|
||||
|
||||
if err = config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&c.opts)
|
||||
if err = o(&c.opts); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if c.opts.Context != nil {
|
||||
@ -54,6 +57,13 @@ func (c *fileConfig) Init(opts ...config.Option) error {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
@ -61,7 +71,7 @@ func (c *fileConfig) Init(opts ...config.Option) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
||||
func (c *fileConfig) Load(ctx context.Context, opts ...options.Option) error {
|
||||
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
|
||||
return nil
|
||||
}
|
||||
@ -90,7 +100,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
|
||||
var fp io.Reader
|
||||
var err error
|
||||
|
||||
if path != "" {
|
||||
if c.path != "" {
|
||||
fp, err = os.OpenFile(path, os.O_RDONLY, os.FileMode(0o400))
|
||||
} else if c.reader != nil {
|
||||
fp = reader
|
||||
@ -124,7 +134,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
|
||||
r = fp
|
||||
}
|
||||
|
||||
buf, err := io.ReadAll(io.LimitReader(r, MaxFileSize))
|
||||
buf, err := io.ReadAll(io.LimitReader(r, int64(codec.DefaultMaxMsgSize)))
|
||||
if err != nil {
|
||||
if !c.opts.AllowFail {
|
||||
return err
|
||||
@ -168,7 +178,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
||||
func (c *fileConfig) Save(ctx context.Context, opts ...options.Option) error {
|
||||
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
|
||||
return nil
|
||||
}
|
||||
@ -238,7 +248,7 @@ func (c *fileConfig) Name() string {
|
||||
return c.opts.Name
|
||||
}
|
||||
|
||||
func (c *fileConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) {
|
||||
func (c *fileConfig) Watch(ctx context.Context, opts ...options.Option) (config.Watcher, error) {
|
||||
path := c.path
|
||||
options := config.NewWatchOptions(opts...)
|
||||
if options.Context != nil {
|
||||
@ -261,7 +271,7 @@ func (c *fileConfig) Watch(ctx context.Context, opts ...config.WatchOption) (con
|
||||
return w, nil
|
||||
}
|
||||
|
||||
func NewConfig(opts ...config.Option) config.Config {
|
||||
func NewConfig(opts ...options.Option) config.Config {
|
||||
options := config.NewOptions(opts...)
|
||||
if len(options.StructTag) == 0 {
|
||||
options.StructTag = DefaultStructTag
|
||||
|
20
file_test.go
20
file_test.go
@ -4,11 +4,13 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"go.unistack.org/micro/v3/codec"
|
||||
"go.unistack.org/micro/v3/config"
|
||||
"go.unistack.org/micro/v4/codec"
|
||||
"go.unistack.org/micro/v4/config"
|
||||
"go.unistack.org/micro/v4/options"
|
||||
)
|
||||
|
||||
type jsoncodec struct{}
|
||||
@ -21,10 +23,22 @@ func (*jsoncodec) Unmarshal(buf []byte, v interface{}, opts ...codec.Option) err
|
||||
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 {
|
||||
return "json"
|
||||
}
|
||||
|
||||
func (*jsoncodec) Write(w io.Writer, m *codec.Message, v interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestLoadReplace(t *testing.T) {
|
||||
type Config struct {
|
||||
Key string
|
||||
@ -38,7 +52,7 @@ func TestLoadReplace(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c := NewConfig(config.Codec(
|
||||
c := NewConfig(options.Codec(
|
||||
&jsoncodec{}),
|
||||
config.Struct(cfg),
|
||||
Reader(buf),
|
||||
|
22
go.mod
22
go.mod
@ -1,22 +1,10 @@
|
||||
module go.unistack.org/micro-config-file/v3
|
||||
module go.unistack.org/micro-config-file/v4
|
||||
|
||||
go 1.22
|
||||
|
||||
toolchain go1.23.1
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
dario.cat/mergo v1.0.1
|
||||
go.unistack.org/micro/v3 v3.11.14
|
||||
golang.org/x/text v0.21.0
|
||||
dario.cat/mergo v1.0.0
|
||||
go.unistack.org/micro/v4 v4.0.17
|
||||
)
|
||||
|
||||
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
|
||||
)
|
||||
require github.com/google/uuid v1.6.0 // indirect
|
||||
|
30
go.sum
30
go.sum
@ -1,31 +1,9 @@
|
||||
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
|
||||
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=
|
||||
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
|
||||
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
|
||||
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/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
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=
|
||||
go.unistack.org/micro/v4 v4.0.17 h1:mF7uM+J4ILdG+1fcwzKYCwDlxhdbF/e1WnGzKKLnIXc=
|
||||
go.unistack.org/micro/v4 v4.0.17/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
|
||||
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/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
26
options.go
26
options.go
@ -5,40 +5,28 @@ import (
|
||||
"os"
|
||||
"regexp"
|
||||
|
||||
"go.unistack.org/micro/v3/config"
|
||||
"go.unistack.org/micro/v4/options"
|
||||
"golang.org/x/text/transform"
|
||||
)
|
||||
|
||||
type pathKey struct{}
|
||||
|
||||
func Path(path string) config.Option {
|
||||
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)
|
||||
func Path(path string) options.Option {
|
||||
return options.ContextOption(pathKey{}, path)
|
||||
}
|
||||
|
||||
type readerKey struct{}
|
||||
|
||||
func Reader(r io.Reader) config.Option {
|
||||
return config.SetOption(readerKey{}, r)
|
||||
func Reader(r io.Reader) options.Option {
|
||||
return options.ContextOption(readerKey{}, r)
|
||||
}
|
||||
|
||||
type transformerKey struct{}
|
||||
|
||||
type TransformerFunc func(src []byte, index []int) []byte
|
||||
|
||||
func Transformer(t transform.Transformer) config.Option {
|
||||
return config.SetOption(transformerKey{}, t)
|
||||
func Transformer(t transform.Transformer) options.Option {
|
||||
return options.ContextOption(transformerKey{}, t)
|
||||
}
|
||||
|
||||
func NewEnvTransformer(rs string, trimLeft, trimRight int) (*EnvTransformer, error) {
|
||||
|
@ -6,9 +6,10 @@ import (
|
||||
"os"
|
||||
"reflect"
|
||||
|
||||
"go.unistack.org/micro/v3/config"
|
||||
"go.unistack.org/micro/v3/util/jitter"
|
||||
rutil "go.unistack.org/micro/v3/util/reflect"
|
||||
"go.unistack.org/micro/v4/codec"
|
||||
"go.unistack.org/micro/v4/config"
|
||||
"go.unistack.org/micro/v4/util/jitter"
|
||||
rutil "go.unistack.org/micro/v4/util/reflect"
|
||||
)
|
||||
|
||||
type fileWatcher struct {
|
||||
@ -42,7 +43,7 @@ func (w *fileWatcher) run() {
|
||||
return
|
||||
}
|
||||
var buf []byte
|
||||
buf, err = io.ReadAll(io.LimitReader(fp, MaxFileSize))
|
||||
buf, err = io.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
|
||||
if err == nil {
|
||||
err = w.opts.Codec.Unmarshal(buf, dst)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user