Skip to content

Commit

Permalink
Fix a bug : can't use print Function for raw value
Browse files Browse the repository at this point in the history
  • Loading branch information
WAY29 committed Nov 18, 2020
1 parent 6e672db commit feb87ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
22 changes: 13 additions & 9 deletions icecream/icecream.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@ func printMsg(msg interface{}) {
}
}

func printValue(v interface{}) {
raf := argToStringFunction
rafk := raf.Kind()
if rafk == reflect.Invalid || rafk == reflect.Bool { // ? argToStringFunction default is fmt.Sprintf
printMsg(fmt.Sprintf("%#v", v))
} else if rafk == reflect.Func { // ? call custom argToStringFunction
results := raf.Call([]reflect.Value{reflect.ValueOf(v)})
printMsg(results[0].Interface())
}
}

func Ic(values ...interface{}) {
var msg string
line := 0
pc, filename, line, ok := runtime.Caller(1)
funcName := runtime.FuncForPC(pc).Name()
pwd, _ := os.Getwd()
relFilename, _ := filepath.Rel(pwd, filename)
raf := argToStringFunction
rafk := raf.Kind()
if ok {
lenOfValues := len(values)
if lenOfValues > 0 { // ? print value
Expand All @@ -45,16 +54,11 @@ func Ic(values ...interface{}) {
vName := reflectsource.GetParentArgExprAsString(uint32(i))
fmtV := fmt.Sprintf("%#v", v)
if vName == fmtV { // ? vName the same as value, just print one
printMsg(fmtV)
printValue(v)
} else {
msg = fmt.Sprintf("%s: ", vName)
printMsg(msg)
if rafk == reflect.Invalid || rafk == reflect.Bool { // ? argToStringFunction default is fmt.Sprintf
printMsg(fmt.Sprintf("%#v", v))
} else if rafk == reflect.Func { // ? call custom argToStringFunction
results := raf.Call([]reflect.Value{reflect.ValueOf(v)})
printMsg(results[0].Interface())
}
printValue(v)
}
if i < lenOfValues-1 { // ? print split character
printMsg(", ")
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
ConfigureArgToStringFunction(toString)
ConfigureIncludeContext(true)
Ic(thisIsFUnny, funny)
Ic(1)
Ic(1, 2, "Test")
Ic(add(123))
test()
}

0 comments on commit feb87ae

Please sign in to comment.