Skip to content

Commit

Permalink
Merge pull request #33 from goreflect/issue_30
Browse files Browse the repository at this point in the history
corrected logic for configuring values by pipeline
  • Loading branch information
artemkaxboy committed Sep 27, 2020
2 parents d993da2 + 52cbac4 commit 717c0e3
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 9 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ module github.com/goreflect/gostructor
go 1.13

require (
github.com/google/go-cmp v0.5.2 // indirect
github.com/goreflect/go_hocon v0.0.2
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.5.0
github.com/stretchr/testify v1.5.1
gotest.tools v2.2.0+incompatible
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ github.com/go-akka/configuration v0.0.0-20190919102339-a31c845c4b1b h1:3tSuByOnO
github.com/go-akka/configuration v0.0.0-20190919102339-a31c845c4b1b/go.mod h1:19bUnum2ZAeftfwwLZ/wRe7idyfoW2MfmXO464Hrfbw=
github.com/go-akka/configuration v0.0.0-20200115015912-550403a6bd87 h1:qsA6HPoRXYJ1a2fsHVUmieRTg+otMAM4wEJgSWzumL8=
github.com/go-akka/configuration v0.0.0-20200115015912-550403a6bd87/go.mod h1:19bUnum2ZAeftfwwLZ/wRe7idyfoW2MfmXO464Hrfbw=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/goreflect/go_hocon v0.0.1 h1:n05o2/7F4S2Q6ZOnn02Sm3kEj/zmkvThk2m7uQQ1Ong=
github.com/goreflect/go_hocon v0.0.1/go.mod h1:+Ty2MKNMIhPNLYWpO4BqpH0W9qZOhKB/upHgEYAlRB8=
github.com/goreflect/go_hocon v0.0.2 h1:8r5kdjE4iG+Hfv1qScSR2LUNcwUwTmFdqpCN/yKY/uA=
github.com/goreflect/go_hocon v0.0.2/go.mod h1:Pw26F1wCoERtcchtIfjcTLO/wb9slw11ZSCvUbOgrYU=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
Expand All @@ -31,6 +35,10 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gotest.tools v1.4.0 h1:BjtEgfuw8Qyd+jPvQz8CfoxiO/UjFEidWinwEXZiWv0=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
20 changes: 20 additions & 0 deletions lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ type (
Field4 float32 `cf_env:"myField4"`
Field5 []bool `cf_env:"myField5"`
}

ManySourceStrategies struct {
Field1 int16 `cf_env:"myField1" cf_hocon:"field2" cf_default:"14"`
Field2 string `cf_hocon:"field2" cf_default:"test_tratata" cf_env:"test_tururu"`
}
)

func Test_parseHocon1(t *testing.T) {
Expand Down Expand Up @@ -179,3 +184,18 @@ func TestChangeLogsParams(t *testing.T) {
ChangeLogLevel(logrus.DebugLevel)
ChangeLogFormatter(&logrus.JSONFormatter{})
}

func Test_covarianceSources(t *testing.T) {
os.Setenv("test_tururu", "tururum")
defer func() {
os.Unsetenv("test_tururu")
}()

myStruct, err := ConfigureSmart(&ManySourceStrategies{}, "")
if err != nil {
t.Error(err)
return
}

t.Log(myStruct)
}
5 changes: 4 additions & 1 deletion pipeline/environment_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ func (config EnvironmentConfig) GetBaseType(context *structContext) infra.GoStru
valueIndirect := reflect.Indirect(context.Value)
valueTag := context.StructField.Tag.Get(tags.TagEnvironment)

if valueTag != "" {
if !config.checkTagValue(valueTag) {
value := os.Getenv(valueTag)
if value == "" {
return infra.NewGoStructorNoValue(context.Value, errors.New("Readed value from environment was empty"))
}
return converters.ConvertBetweenPrimitiveTypes(reflect.ValueOf(value), valueIndirect)
}
return infra.NewGoStructorNoValue(context.Value, errors.New("getBaseType can not get field by empty tag value of tag: "+tags.TagEnvironment))
Expand Down
43 changes: 35 additions & 8 deletions pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type (
chains *Chain
errors []string
sourcesTypes []int
curentChain *Chain
}

/*Chain - this is structure contain information for executing function by ordering
Expand Down Expand Up @@ -226,10 +227,18 @@ func (pipeline *Pipeline) recursiveParseFields(context *structContext) error {
case reflect.Struct:
return pipeline.preparedInlineStructFields(valuePtr, context)
default:
if err := pipeline.configuringValues(context); err != nil {
pipeline.addNewErrorWhileParsing(err.Error())
pipeline.curentChain = pipeline.chains
for {
if err := pipeline.configuringValues(context); err != nil {
pipeline.addNewErrorWhileParsing(err.Error())
if errSettingChain := pipeline.setNextChain(); errSettingChain != nil {
return pipeline.getErrorAsOne()
}
continue
}
break
}
return pipeline.getErrorAsOne()
return nil
}
}

Expand Down Expand Up @@ -274,14 +283,20 @@ func (pipeline *Pipeline) configuringValues(context *structContext) error {
valueIndirect := reflect.Indirect(context.Value)
switch valueIndirect.Kind() {
case reflect.Slice, reflect.Map, reflect.Array:
valueGet := pipeline.chains.stageFunction.GetComplexType(context)
logrus.Debug("value get from parsing slice: ", valueGet)
return pipeline.setupValue(context, &valueGet)
if pipeline.curentChain != nil {
valueGet := pipeline.curentChain.stageFunction.GetComplexType(context)
logrus.Debug("value get from parsing slice: ", valueGet)
return pipeline.setupValue(context, &valueGet)
}
return errors.New("can not be configuring complex type. Can not configured current field")
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return errors.New("not implemented types of unsigned integer")
case reflect.String, reflect.Float32, reflect.Float64, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
valueGet := pipeline.chains.stageFunction.GetBaseType(context)
return pipeline.setupValue(context, &valueGet)
if pipeline.curentChain != nil {
valueGet := pipeline.curentChain.stageFunction.GetBaseType(context)
return pipeline.setupValue(context, &valueGet)
}
return errors.New("can not be configuring base type. Can not configured current field")
default:
return errors.New("not supported type for hocon parsing")
}
Expand Down Expand Up @@ -316,3 +331,15 @@ func (pipeline *Pipeline) checkValuePrefix(prefix string) error {
}
return nil
}

func (pipeline *Pipeline) setNextChain() error {
if pipeline.curentChain == nil {
pipeline.curentChain = pipeline.chains
return nil
}
if pipeline.curentChain.next == nil {
return errors.New("can not change chain function")
}
pipeline.curentChain = pipeline.curentChain.next
return nil
}
65 changes: 65 additions & 0 deletions pipeline/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,3 +690,68 @@ func TestPipeline_recursiveParseFields(t *testing.T) {
})
}
}

func TestPipeline_setNextChain(t *testing.T) {
type fields struct {
chains *Chain
errors []string
sourcesTypes []int
curentChain *Chain
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
name: "correct changed current stage function",
fields: fields{
chains: &Chain{
stageFunction: EnvironmentConfig{},
next: nil,
},
errors: nil,
sourcesTypes: nil,
curentChain: nil,
},
wantErr: false,
},
{
name: "incorrect change current stage function",
fields: fields{
chains: &Chain{
next: nil,
},
curentChain: &Chain{
stageFunction: EnvironmentConfig{},
},
},
wantErr: true,
},
{
name: "correct change current stage function",
fields: fields{
curentChain: &Chain{
stageFunction: EnvironmentConfig{},
next: &Chain{
stageFunction: &DefaultConfig{},
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pipeline := &Pipeline{
chains: tt.fields.chains,
errors: tt.fields.errors,
sourcesTypes: tt.fields.sourcesTypes,
curentChain: tt.fields.curentChain,
}
if err := pipeline.setNextChain(); (err != nil) != tt.wantErr {
t.Errorf("Pipeline.setNextChain() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
65 changes: 65 additions & 0 deletions tags/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tags

import (
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -145,3 +146,67 @@ func TestParser_Scan(t *testing.T) {
// parsedSlice,
// )
}

func TestParser_parsePathAsSingleValue(t *testing.T) {
type fields struct {
Lexer *Scanner
Buffer struct{ AmountLetters int }
Term TerminalSymbol
}
tests := []struct {
name string
fields fields
wantPath Path
wantErr bool
}{
{
name: "completed parsed path",
fields: fields{
Lexer: &Scanner{
r: nil,
CurrentPosition: 0,
},
Buffer: struct{ AmountLetters int }{
AmountLetters: 1,
},
Term: TerminalSymbol{
Tok: FUNCTION,
Literal: "test",
StartPositioin: 0,
EndPosition: 4,
},
},
wantPath: Path{
TerminalSymbol: TerminalSymbol{
Tok: PATH,
Literal: "test",
StartPositioin: 0,
EndPosition: 4,
},
PathName: TerminalSymbol{
Tok: FUNCTION,
Literal: "test",
StartPositioin: 0,
EndPosition: 4,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser := &Parser{
Lexer: tt.fields.Lexer,
Buffer: tt.fields.Buffer,
Term: tt.fields.Term,
}
gotPath, err := parser.parsePathAsSingleValue()
if (err != nil) != tt.wantErr {
t.Errorf("Parser.parsePathAsSingleValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotPath, tt.wantPath) {
t.Errorf("Parser.parsePathAsSingleValue() = %v, want %v", gotPath, tt.wantPath)
}
})
}
}

0 comments on commit 717c0e3

Please sign in to comment.