fieldalignment of all structs to save memory

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-04-27 08:32:47 +03:00
parent ee11f39a2f
commit 86626c5922
78 changed files with 343 additions and 410 deletions

View File

@@ -6,10 +6,8 @@ import (
"errors"
)
var (
// DefaultConfig default config
DefaultConfig Config = NewConfig()
)
// DefaultConfig default config
var DefaultConfig Config = NewConfig()
var (
// ErrCodecMissing is returned when codec needed and not specified
@@ -37,10 +35,10 @@ type Config interface {
}
// Watcher is the config watcher
//type Watcher interface {
// type Watcher interface {
// Next() (, error)
// Stop() error
//}
// }
// Load loads config from config sources
func Load(ctx context.Context, cs ...Config) error {

View File

@@ -35,7 +35,7 @@ func (c *defaultConfig) Load(ctx context.Context) error {
src, err := rutil.Zero(c.opts.Struct)
if err == nil {
valueOf := reflect.ValueOf(src)
if err = c.fillValues(ctx, valueOf); err == nil {
if err = c.fillValues(valueOf); err == nil {
err = mergo.Merge(c.opts.Struct, src, mergo.WithOverride, mergo.WithTypeCheck, mergo.WithAppendSlice)
}
}
@@ -54,7 +54,7 @@ func (c *defaultConfig) Load(ctx context.Context) error {
}
//nolint:gocyclo
func (c *defaultConfig) fillValue(ctx context.Context, value reflect.Value, val string) error {
func (c *defaultConfig) fillValue(value reflect.Value, val string) error {
if !rutil.IsEmpty(value) {
return nil
}
@@ -71,10 +71,10 @@ func (c *defaultConfig) fillValue(ctx context.Context, value reflect.Value, val
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 := c.fillValue(mkey, kv[0]); err != nil {
return err
}
if err := c.fillValue(ctx, mval, kv[1]); err != nil {
if err := c.fillValue(mval, kv[1]); err != nil {
return err
}
value.SetMapIndex(mkey, mval)
@@ -84,7 +84,7 @@ func (c *defaultConfig) fillValue(ctx context.Context, value reflect.Value, val
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 := c.fillValue(nvalue, nval); err != nil {
return err
}
value.Index(idx).Set(nvalue)
@@ -173,7 +173,7 @@ func (c *defaultConfig) fillValue(ctx context.Context, value reflect.Value, val
return nil
}
func (c *defaultConfig) fillValues(ctx context.Context, valueOf reflect.Value) error {
func (c *defaultConfig) fillValues(valueOf reflect.Value) error {
var values reflect.Value
if valueOf.Kind() == reflect.Ptr {
@@ -200,7 +200,7 @@ func (c *defaultConfig) fillValues(ctx context.Context, valueOf reflect.Value) e
switch value.Kind() {
case reflect.Struct:
value.Set(reflect.Indirect(reflect.New(value.Type())))
if err := c.fillValues(ctx, value); err != nil {
if err := c.fillValues(value); err != nil {
return err
}
continue
@@ -214,7 +214,7 @@ func (c *defaultConfig) fillValues(ctx context.Context, valueOf reflect.Value) e
value.Set(reflect.New(value.Type().Elem()))
}
value = value.Elem()
if err := c.fillValues(ctx, value); err != nil {
if err := c.fillValues(value); err != nil {
return err
}
continue
@@ -224,7 +224,7 @@ func (c *defaultConfig) fillValues(ctx context.Context, valueOf reflect.Value) e
continue
}
if err := c.fillValue(ctx, value, tag); err != nil {
if err := c.fillValue(value, tag); err != nil {
return err
}
}

View File

@@ -10,30 +10,30 @@ import (
type Cfg struct {
StringValue string `default:"string_value"`
IntValue int `default:"99"`
IgnoreValue string `json:"-"`
StructValue struct {
StringValue string `default:"string_value"`
}
IntValue int `default:"99"`
}
func TestDefault(t *testing.T) {
ctx := context.Background()
conf := &Cfg{IntValue: 10}
blfn := func(ctx context.Context, cfg config.Config) error {
conf, ok := cfg.Options().Struct.(*Cfg)
nconf, ok := cfg.Options().Struct.(*Cfg)
if !ok {
return fmt.Errorf("failed to get Struct from options: %v", cfg.Options())
}
conf.StringValue = "before_load"
nconf.StringValue = "before_load"
return nil
}
alfn := func(ctx context.Context, cfg config.Config) error {
conf, ok := cfg.Options().Struct.(*Cfg)
nconf, ok := cfg.Options().Struct.(*Cfg)
if !ok {
return fmt.Errorf("failed to get Struct from options: %v", cfg.Options())
}
conf.StringValue = "after_load"
nconf.StringValue = "after_load"
return nil
}

View File

@@ -7,9 +7,9 @@ import (
)
type Config struct {
Value string
SubConfig *SubConfig
Config *Config
Value string
}
type SubConfig struct {