Skip to content

Commit

Permalink
Merge pull request #23 from ThomasObenaus/b/14_code_smells
Browse files Browse the repository at this point in the history
B/14 code smells
  • Loading branch information
ThomasObenaus authored Feb 23, 2021
2 parents 7b513d3 + 7f401e5 commit 2729652
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
5 changes: 3 additions & 2 deletions apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func applyConfig(provider interfaces.Provider, target interface{}, nameOfParentT
provider.Log(interfaces.LogLevel_Debug, "%s field-type=%s field-value=%v\n", logPrefix, fieldType, fieldValue)

// handling of non primitives (stucts) that have annotated fields
if !isPrimitive && !cfgTag.isComplexTypeWithoutAnnotatedFields {
applyConfigOfSubFields := !isPrimitive && !cfgTag.isComplexTypeWithoutAnnotatedFields
if applyConfigOfSubFields {
fieldValueIf := fieldValue.Addr().Interface()
if err := applyConfig(provider, fieldValueIf, nameOfParentType, cfgTag, mappingFuncs); err != nil {
return errors.Wrap(err, "Applying non primitive")
Expand All @@ -61,7 +62,6 @@ func applyConfig(provider interfaces.Provider, target interface{}, nameOfParentT
}

valueIsSet := provider.IsSet(cfgTag.Name)

if !valueIsSet && cfgTag.IsRequired() {
return fmt.Errorf("Missing required config parameter %s (field: %s)", cfgTag.Name, fieldName)
}
Expand Down Expand Up @@ -105,6 +105,7 @@ func applyConfig(provider interfaces.Provider, target interface{}, nameOfParentT
if mappingFunc == nil && hasMappingFunc {
return fmt.Errorf("Mapping func '%s' not found", mappingFuncName)
}

if mappingFunc != nil {
provider.Log(interfaces.LogLevel_Debug, "%s apply mapping function '%s' (%s())\n", logPrefix, cfgTag.MapFunName, resolvedMappingFuncName)
mappedValue, err := mappingFunc(val, fieldType)
Expand Down
9 changes: 5 additions & 4 deletions cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,12 @@ func createAndFillStruct(targetTypeOfStruct reflect.Type, data map[string]interf
return reflect.Zero(targetTypeOfStruct), errors.Wrapf(err, "Parsing configTag '%s'", configTag)
}
val, ok := data[entry.Name]
if !ok {
if entry.IsRequired() {
return reflect.Zero(targetTypeOfStruct), fmt.Errorf("Missing value for required field (struct-field='%s',expected-key='%s')", fieldDeclaration.Name, entry.Name)
}

if !ok && entry.IsRequired() {
return reflect.Zero(targetTypeOfStruct), fmt.Errorf("Missing value for required field (struct-field='%s',expected-key='%s')", fieldDeclaration.Name, entry.Name)
}

if !ok {
// take the default value
val = entry.Def
}
Expand Down
2 changes: 0 additions & 2 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ func registerFlag(flagSet *pflag.FlagSet, entry Entry) error {
}
}

// TODO: Regard default value and set it when registering the flag

switch castedDefaultValue := valueDesiredType.(type) {
case string:
flagSet.StringP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
Expand Down
11 changes: 7 additions & 4 deletions interfaces/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ const (
LogLevel_Error LogLevel = "Error"
)

//nolint
const formatString = "[%s] %s"

type LoggerFunc func(lvl LogLevel, formatString string, a ...interface{})

// DebugLogger logs all messages of level debug or above
var DebugLogger = func(lvl LogLevel, formatString string, a ...interface{}) {
msg := fmt.Sprintf(formatString, a...)
fmt.Printf("[%s] %s", lvl, msg)
fmt.Printf(formatString, lvl, msg)
}

// InfoLogger logs all messages of level info or above
Expand All @@ -25,7 +28,7 @@ var InfoLogger = func(lvl LogLevel, formatString string, a ...interface{}) {
return
}
msg := fmt.Sprintf(formatString, a...)
fmt.Printf("[%s] %s", lvl, msg)
fmt.Printf(formatString, lvl, msg)
}

// WarnLogger logs all messages of level warn or above
Expand All @@ -34,7 +37,7 @@ var WarnLogger = func(lvl LogLevel, formatString string, a ...interface{}) {
return
}
msg := fmt.Sprintf(formatString, a...)
fmt.Printf("[%s] %s", lvl, msg)
fmt.Printf(formatString, lvl, msg)
}

// ErrorLogger logs all messages of level error
Expand All @@ -43,7 +46,7 @@ var ErrorLogger = func(lvl LogLevel, formatString string, a ...interface{}) {
return
}
msg := fmt.Sprintf(formatString, a...)
fmt.Printf("[%s] %s", lvl, msg)
fmt.Printf(formatString, lvl, msg)
}

var NoLogging = func(lvl LogLevel, formatString string, a ...interface{}) {
Expand Down

0 comments on commit 2729652

Please # to comment.