Skip to content

Commit

Permalink
fixed, enhancing, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kubitre committed May 5, 2021
1 parent 0dd989d commit 17f050e
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 29 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,26 @@ jobs:
- name: Run tests
run: go test ./... -v -covermode=count

publishing:
runs-on: ubuntu-18.04
steps:
- name: checkout to master
uses: actions/checkout@master
- name: checkout to current branch
run:
git checkout ${{github.ref.branch}}
- name: Commit new patch version
run: |
git config --local user.name "kubitre bot"
git config --local user.email "kubitre@gmail.com"
./increment_version.bash
git diff-index --quiet HEAD || git commit -m "upgrade patch" -a
git tag $(cat .version)
git push -q origin $(cat .version)
- name: push lib to github repo
uses: ad-m/github-push-action@master
with:
github_token: ${{secrets.GITHUB_TOKEN}}
force: true


24 changes: 0 additions & 24 deletions .github/workflows/publish_patch.yaml

This file was deleted.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Universal configuration library by tags
## Current supporting types

- int, int8, int16, int32, int64
- uint, uint8, uint16, uint32, uint64
- float32, float64
- string
- bool
Expand Down
28 changes: 28 additions & 0 deletions converters/base_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,31 @@ func choseTypeByOrder(value float64, order int) reflect.Value {
}
return reflect.ValueOf(value)
}

func convertToUintOrder(source reflect.Value, destination reflect.Value, order int) infra.GoStructorValue {
switch source.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return infra.NewGoStructorTrueValue(chooseTypeByOrder(source.Uint(), order))
case reflect.String:
parsed, errParsed := strconv.ParseUint(source.String(), 10, order)
if errParsed != nil {
return infra.NewGoStructorNoValue(destination, errParsed)
}
return infra.NewGoStructorTrueValue(chooseTypeByOrder(parsed, order))
default:
return infra.NewGoStructorNoValue(source, errors.New("not supported convertation"))
}
}

func chooseTypeByOrder(value uint64, order int) reflect.Value {
switch order {
case 8:
return reflect.ValueOf(uint8(value))
case 16:
return reflect.ValueOf(uint16(value))
case 32:
return reflect.ValueOf(uint32(value))
default:
return reflect.ValueOf(value)
}
}
4 changes: 4 additions & 0 deletions converters/base_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,7 @@ func Test_convertToFloat64(t *testing.T) {
})
}
}

func Test_convertToUint8(t *testing.T) {

}
10 changes: 10 additions & 0 deletions converters/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ func ConvertBetweenPrimitiveTypes(source reflect.Value, destination reflect.Valu
return convertToIntOrder(source, destination, 32)
case reflect.Int64:
return convertToIntOrder(source, destination, 64)
case reflect.Uint:
return convertToUintOrder(source, destination, 32)
case reflect.Uint8:
return convertToUintOrder(source, destination, 8)
case reflect.Uint16:
return convertToUintOrder(source, destination, 16)
case reflect.Uint32:
return convertToUintOrder(source, destination, 32)
case reflect.Uint64:
return convertToUintOrder(source, destination, 64)
case reflect.String:
return convertToString(source, destination)
case reflect.Float32:
Expand Down
28 changes: 23 additions & 5 deletions pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"reflect"
"strings"
"unsafe"

"github.com/goreflect/gostructor/infra"
"github.com/goreflect/gostructor/tags"
Expand Down Expand Up @@ -275,7 +276,6 @@ func (pipeline *Pipeline) recursiveParseFields(context *structContext) error {
pipeline.extractOrderChainOrUseSettingUpChains(context)
for {
if err := pipeline.configuringValues(context); err != nil {
pipeline.addNewErrorWhileParsing(err.Error())
if errSettingChain := pipeline.setNextChain(); errSettingChain != nil {
return pipeline.getErrorAsOne()
}
Expand Down Expand Up @@ -348,9 +348,21 @@ func (pipeline *Pipeline) configuringValues(context *structContext) error {
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:
case
reflect.String,
reflect.Float32,
reflect.Float64,
reflect.Bool,
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
if pipeline.curentChain != nil {
valueGet := pipeline.curentChain.stageFunction.GetBaseType(context)
return pipeline.setupValue(context, &valueGet)
Expand All @@ -370,7 +382,13 @@ func (pipeline *Pipeline) setupValue(context *structContext, value *infra.GoStru
valueIndirect.Set(value.Value)
return nil
}
return errors.New("can not set " + value.Value.Kind().String() + " into struct field.")
// change data by address
logrus.Info(context.Value.Type())
logrus.Info(valueIndirect.Type())
unsafeValue := reflect.NewAt(valueIndirect.Type(), unsafe.Pointer(valueIndirect.UnsafeAddr())).Elem()
unsafeValue.Set(value.Value)
return nil

}

return errors.New("Loglevel: Debug Message: value get not implementedable value: ")
Expand Down
64 changes: 64 additions & 0 deletions pipeline/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,3 +706,67 @@ func TestPipeline_setNextChain(t *testing.T) {
})
}
}

type TestStructPriority struct {
field1 string `cf_default:"tururu" cf_env:"MY_TEST" cf_priority:"stage1:cf_env,cf_default;stage2:cf_default,cf_env"`
}

func TestPipelineOrderConfiguring(t *testing.T) {
type args struct {
structure interface{}
fileName string
pipelineChaines []infra.FuncType
prefix string
smartConfigure bool
}

myTestStruct := TestStructPriority{}
os.Setenv("MY_TEST", "TURURU")
tests := []struct {
name string
args args
wantResult interface{}
wantErr bool
setPriority string
}{
{
name: "success configuring from cf_env",
args: args{
structure: &myTestStruct,
fileName: "",
pipelineChaines: []infra.FuncType{infra.FunctionSetupDefault},
prefix: "",
smartConfigure: true,
},
wantResult: "TURURU",
wantErr: false,
setPriority: "stage1",
},
{
name: "success configuring from cf_default",
args: args{
structure: &myTestStruct,
fileName: "test",
pipelineChaines: []infra.FuncType{infra.FunctionSetupJSON},
prefix: "",
smartConfigure: true,
},
wantResult: "tururu",
wantErr: false,
setPriority: "stage2",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Setenv("GOSTRUCTOR_PRIORITY", tt.setPriority)
gotResult, err := Configure(tt.args.structure, tt.args.pipelineChaines, tt.args.prefix, tt.args.smartConfigure)
if (err != nil) != tt.wantErr {
t.Errorf("Configure() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotResult.(*TestStructPriority).field1 != tt.wantResult {
t.Errorf("Not equaled result, ordering not working")
}
})
}
}

0 comments on commit 17f050e

Please sign in to comment.