Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
89b41364c3 | |||
d7ffcd19f8 |
21
file.go
21
file.go
@@ -8,13 +8,15 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"dario.cat/mergo"
|
"dario.cat/mergo"
|
||||||
"go.unistack.org/micro/v3/codec"
|
|
||||||
"go.unistack.org/micro/v3/config"
|
"go.unistack.org/micro/v3/config"
|
||||||
rutil "go.unistack.org/micro/v3/util/reflect"
|
rutil "go.unistack.org/micro/v3/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
|
||||||
@@ -76,11 +78,20 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
path := c.path
|
path := c.path
|
||||||
|
transformer := c.transformer
|
||||||
|
reader := c.reader
|
||||||
|
|
||||||
options := config.NewLoadOptions(opts...)
|
options := config.NewLoadOptions(opts...)
|
||||||
if options.Context != nil {
|
if options.Context != nil {
|
||||||
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
|
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
|
||||||
path = v
|
path = v
|
||||||
}
|
}
|
||||||
|
if v, ok := c.opts.Context.Value(transformerKey{}).(transform.Transformer); ok {
|
||||||
|
transformer = v
|
||||||
|
}
|
||||||
|
if v, ok := c.opts.Context.Value(readerKey{}).(io.Reader); ok {
|
||||||
|
reader = v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var fp io.Reader
|
var fp io.Reader
|
||||||
@@ -89,7 +100,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
|
|||||||
if c.path != "" {
|
if c.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 = c.reader
|
fp = reader
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("Path or Reader must be specified")
|
err = fmt.Errorf("Path or Reader must be specified")
|
||||||
}
|
}
|
||||||
@@ -114,13 +125,13 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
var r io.Reader
|
var r io.Reader
|
||||||
if c.transformer != nil {
|
if transformer != nil {
|
||||||
r = transform.NewReader(fp, c.transformer)
|
r = transform.NewReader(fp, c.transformer)
|
||||||
} else {
|
} else {
|
||||||
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
|
||||||
|
13
file_test.go
13
file_test.go
@@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -22,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
|
||||||
|
@@ -7,7 +7,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/codec"
|
|
||||||
"go.unistack.org/micro/v3/config"
|
"go.unistack.org/micro/v3/config"
|
||||||
"go.unistack.org/micro/v3/util/jitter"
|
"go.unistack.org/micro/v3/util/jitter"
|
||||||
rutil "go.unistack.org/micro/v3/util/reflect"
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
||||||
@@ -44,7 +43,7 @@ func (w *fileWatcher) run() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err = ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
|
buf, err = ioutil.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)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user