-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from goreflect/issue_8
Issue 8 Resolved
- Loading branch information
Showing
9 changed files
with
261 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package middlewares | ||
|
||
func emptyTagValue(tagValue string) bool { | ||
return tagValue == "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package middlewares | ||
|
||
import "errors" | ||
|
||
/* | ||
ExecutorMiddlewaresByTagValue - entry point for all middlewares | ||
TODO: increase by tagType | ||
*/ | ||
func ExecutorMiddlewaresByTagValue(tagValue string, tagType string) error { | ||
if emptyTagValue(tagValue) { | ||
return errors.New("tagvalue can not be empty! ") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package pipeline | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/goreflect/gostructor/infra" | ||
) | ||
|
||
func TestDefaultConfig_GetBaseType(t *testing.T) { | ||
strct := struct { | ||
field1 int8 `cf_default:"5"` | ||
}{} | ||
fieldType := reflect.ValueOf(strct).Type().Field(0) | ||
fieldValue := reflect.ValueOf(strct).Field(0) | ||
type args struct { | ||
context *structContext | ||
} | ||
tests := []struct { | ||
name string | ||
config DefaultConfig | ||
args args | ||
want infra.GoStructorValue | ||
}{ | ||
{ | ||
name: "check while configuring base type int8", | ||
args: args{ | ||
context: &structContext{ | ||
Value: fieldValue, | ||
StructField: fieldType, | ||
}, | ||
}, | ||
want: infra.NewGoStructorTrueValue(reflect.ValueOf(int8(5))), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
config := DefaultConfig{} | ||
if got := config.GetBaseType(tt.args.context); !reflect.DeepEqual(got.Value, tt.want.Value) { | ||
t.Errorf("DefaultConfig.GetBaseType() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDefaultConfig_GetBaseTypeFaield(t *testing.T) { | ||
strct := struct { | ||
field1 int8 `cf_default:""` | ||
}{} | ||
fieldType := reflect.ValueOf(strct).Type().Field(0) | ||
fieldValue := reflect.ValueOf(strct).Field(0) | ||
type args struct { | ||
context *structContext | ||
} | ||
tests := []struct { | ||
name string | ||
config DefaultConfig | ||
args args | ||
want infra.GoStructorValue | ||
}{ | ||
{ | ||
name: "check while configuring empty tag Value field", | ||
args: args{ | ||
context: &structContext{ | ||
Value: fieldValue, | ||
StructField: fieldType, | ||
}, | ||
}, | ||
want: infra.NewGoStructorNoValue(fieldValue, errors.New("wrong format inside the tag")), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
config := DefaultConfig{} | ||
if got := config.GetBaseType(tt.args.context); !reflect.DeepEqual(got.Value, tt.want.Value) { | ||
t.Errorf("DefaultConfig.GetBaseType() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDefaultConfig_GetComplexType(t *testing.T) { | ||
strct := struct { | ||
field []int8 `cf_default:"12,2,45,16"` | ||
}{} | ||
fieldType := reflect.ValueOf(strct).Type().Field(0) | ||
fieldValue := reflect.ValueOf(strct).Field(0) | ||
type args struct { | ||
context *structContext | ||
} | ||
tests := []struct { | ||
name string | ||
config DefaultConfig | ||
args args | ||
want infra.GoStructorValue | ||
}{ | ||
{ | ||
name: "get slice from default tag", | ||
config: DefaultConfig{}, | ||
args: args{ | ||
context: &structContext{ | ||
Value: fieldValue, | ||
StructField: fieldType, | ||
}, | ||
}, | ||
want: infra.NewGoStructorTrueValue(reflect.ValueOf([]int8{12, 2, 45, 16})), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
config := DefaultConfig{} | ||
if got := config.GetComplexType(tt.args.context); !reflect.DeepEqual(got.Value.Interface(), tt.want.Value.Interface()) { | ||
t.Errorf("DefaultConfig.GetComplexType() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDefaultConfig_GetComplexTypeNotImlemented(t *testing.T) { | ||
strct := struct { | ||
field map[string]string `cf_default:"12:12sda,51:5sda"` | ||
}{} | ||
fieldType := reflect.ValueOf(strct).Type().Field(0) | ||
fieldValue := reflect.ValueOf(strct).Field(0) | ||
type args struct { | ||
context *structContext | ||
} | ||
tests := []struct { | ||
name string | ||
config DefaultConfig | ||
args args | ||
want infra.GoStructorValue | ||
}{ | ||
{ | ||
name: "get map from default tag not implemented", | ||
config: DefaultConfig{}, | ||
args: args{ | ||
context: &structContext{ | ||
Value: fieldValue, | ||
StructField: fieldType, | ||
}, | ||
}, | ||
want: infra.NewGoStructorNoValue(fieldValue, errors.New("not implemented")), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
config := DefaultConfig{} | ||
if got := config.GetComplexType(tt.args.context); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("DefaultConfig.GetComplexType() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package tools | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
type ( | ||
// ConfigureConverts - configuration structure for converters tools | ||
ConfigureConverts struct { | ||
Separator SeparatorType | ||
} | ||
|
||
// SeparatorType - type for configuring separators | ||
SeparatorType string | ||
) | ||
|
||
const ( | ||
// COMMA - separator in value is , | ||
COMMA SeparatorType = "," | ||
) | ||
|
||
// ConvertStringIntoArray - converting from string into array of string | ||
func ConvertStringIntoArray(value string, configuration ConfigureConverts) ([]string, error) { | ||
if err := configuration.validation(); err != nil { | ||
return nil, err | ||
} | ||
return strings.Split(value, string(configuration.Separator)), nil | ||
} | ||
|
||
func (configure ConfigureConverts) validation() error { | ||
if configure.Separator == "" { | ||
return errors.New("separator can not be empty! ") | ||
} | ||
return nil | ||
} |