Skip to content

Commit

Permalink
completed parser func
Browse files Browse the repository at this point in the history
  • Loading branch information
armada45-pixel committed Dec 31, 2022
1 parent 0180fc2 commit a8740fe
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
11 changes: 9 additions & 2 deletions envstruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Options struct {
OsFirst bool // default false

// Custom parser function for custom type
CustomParserFunc []TypeCustomParserFunc
CustomParserFunc map[reflect.Type]TypeDefaultBy
}

func Setup(optA ...Options) (err []error) {
Expand All @@ -49,14 +49,21 @@ func Setup(optA ...Options) (err []error) {
var varProp = typeVarProp{}
var ref reflect.Value
var errCheckVarWrongType error
var allParserFunc = make(map[reflect.Type]TypeDefaultBy)

if opt.VarPtr != nil {
ref, errCheckVarWrongType = checkVarType(opt.VarPtr)
if errCheckVarWrongType != nil {
err = append(err, errCheckVarWrongType)
} else {
var errCheckVar []error
varProp, errCheckVar = prepareVar(opt.VarPtr, ref)
allParserFunc = DefaultByType
if len(opt.CustomParserFunc) != 0 {
for key, value := range opt.CustomParserFunc {
allParserFunc[key] = value
}
}
varProp, errCheckVar = prepareVar(opt.VarPtr, ref, allParserFunc)
err = append(err, errCheckVar...)
}
}
Expand Down
10 changes: 6 additions & 4 deletions osenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package envstruct

import (
"os"
"reflect"
"strings"
)

type parserOSOption struct {
varProp typeVarProp
envMap map[string]string
opt Options
varProp typeVarProp
envMap map[string]string
opt Options
allParserFunc map[reflect.Kind]TypeDefaultBy
}

func parserOSEnv(opts ...parserOSOption) (varProp typeVarProp, err []error) {
Expand All @@ -28,7 +30,7 @@ func parserOSEnv(opts ...parserOSOption) (varProp typeVarProp, err []error) {
if opt.ReadOS {
prop := varProp.prop[keyProp]
typeVar := prop.refTypeField
newValue, errParserData := parserData(prop.typeProp, typeVar, value)
newValue, errParserData := parserData(prop.typeProp, typeVar, value, opt.CustomParserFunc)
if len(errParserData) != 0 {
err = append(err, errParserData...)
} else {
Expand Down
6 changes: 4 additions & 2 deletions parserfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"errors"
"io"
"os"
"reflect"
"regexp"
"strings"
)

type parserFileOption struct {
varProp typeVarProp
varProp typeVarProp
allParserFunc map[reflect.Type]TypeDefaultBy
}

func parserFile(file io.Reader, opt Options, opts ...parserFileOption) (varProp typeVarProp, envMap map[string]string, err []error) {
Expand All @@ -36,7 +38,7 @@ func parserFile(file io.Reader, opt Options, opts ...parserFileOption) (varProp
if found {
prop := varProp.prop[keyProp]
typeVar := prop.refTypeField
newValue, errParserData := parserData(prop.typeProp, typeVar, value)
newValue, errParserData := parserData(prop.typeProp, typeVar, value, opts[0].allParserFunc)
if len(errParserData) != 0 {
err = append(err, errParserData...)
} else {
Expand Down
4 changes: 2 additions & 2 deletions parservalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"reflect"
)

func parserData(typeKind reflect.Type, typeVar reflect.StructField, value string) (newValue any, err []error) {
func parserData(typeKind reflect.Type, typeVar reflect.StructField, value string, allParserFunc map[reflect.Type]TypeDefaultBy) (newValue any, err []error) {

typeVarKind := typeVar.Type.Kind()
searchDefault, found := defaultByType[typeKind]
searchDefault, found := allParserFunc[typeKind]
if !found {
err = append(err, errors.New("Parser Function For Type "+typeVarKind.String()+" In Field "+typeVar.Name+""))
} else {
Expand Down
4 changes: 2 additions & 2 deletions prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func checkVarType(VarPtr interface{}) (reflect.Value, error) {
return ref, nil
}

func prepareVar(VarPtr interface{}, ref reflect.Value) (ls typeVarProp, err []error) {
func prepareVar(VarPtr interface{}, ref reflect.Value, allParserFunc map[reflect.Type]TypeDefaultBy) (ls typeVarProp, err []error) {

ls = typeVarProp{
check: false,
Expand Down Expand Up @@ -103,7 +103,7 @@ func prepareVar(VarPtr interface{}, ref reflect.Value) (ls typeVarProp, err []er
if defaultValueField_i.IsZero() && (reflect.TypeOf(reflect.Bool) != sometype) {
defaultString := refField.Tag.Get("default")
if len(defaultString) != 0 {
searchDefault, found := defaultByType[sometype]
searchDefault, found := allParserFunc[sometype]
if !found {
err = append(err, errors.New("Parser Function For Type "+sometype.String()+" In Field "+refField.Name+""))
} else {
Expand Down
4 changes: 3 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ type TypeDefaultBy struct {
ValueDefault interface{}
}

// type map[reflect.Type]

var (
// ErrNotAStructPtr is returned if you pass something that is not a pointer to a
// Struct to Parse.
ErrNotAStructPtr = errors.New("env: expected a pointer to a Struct")

defaultByType = map[reflect.Type]TypeDefaultBy{
DefaultByType = map[reflect.Type]TypeDefaultBy{
reflect.TypeOf(false): {
ParserFunc: func(v string) (interface{}, error) {
return strconv.ParseBool(v)
Expand Down

0 comments on commit a8740fe

Please sign in to comment.