Skip to content

Commit

Permalink
fix: func typo
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Aug 27, 2023
1 parent c2bb854 commit 7048ef8
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions decode.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bencode

import (
"errors"
"fmt"
"reflect"
"strconv"
Expand Down Expand Up @@ -162,7 +161,7 @@ type filedInfo struct {
TagName string
}

func scannTagField(field reflect.StructField) filedInfo {
func scanTagField(field reflect.StructField) filedInfo {
alias := field.Name
if tag, ok := field.Tag.Lookup(bencodeSymbol); ok {
tpl := strings.Split(tag, ",")
Expand All @@ -178,23 +177,23 @@ func scannTagField(field reflect.StructField) filedInfo {
}
}

func bindTag(decodedMap map[string]interface{}, stu interface{}) {
func bindTag(decodedMap map[string]interface{}, stu interface{}) error {
stuType := reflect.TypeOf(stu)
stuKind := stuType.Kind()

if stuKind == reflect.Invalid {
return
return fmt.Errorf("can't process empty value")
}

if stuKind != reflect.Ptr {
return
return fmt.Errorf("invalid stu type: should be pointer")
}

stuType = stuType.Elem()
stuKind = stuType.Kind()

if stuKind != reflect.Struct {
return
return fmt.Errorf("invalid stu type: should be struct")
}

stuValue := reflect.ValueOf(stu).Elem()
Expand All @@ -219,7 +218,7 @@ func bindTag(decodedMap map[string]interface{}, stu interface{}) {

for i := 0; i < stuType.NumField(); i++ {
field := stuType.Field(i)
info := scannTagField(field)
info := scanTagField(field)
each := stuValue.FieldByName(info.TagName)
if value, ok := decodedMap[info.Alias]; ok {
t := reflect.TypeOf(value)
Expand All @@ -240,7 +239,7 @@ func bindTag(decodedMap map[string]interface{}, stu interface{}) {

}
}

return nil
}

func UnMarshal(data interface{}, stu interface{}) error {
Expand All @@ -249,7 +248,7 @@ func UnMarshal(data interface{}, stu interface{}) error {
kind := value.Kind()

if kind == reflect.Invalid {
return errors.New("can't process empty value")
return fmt.Errorf("can't process empty value")
}

if kind != reflect.Map {
Expand All @@ -262,6 +261,5 @@ func UnMarshal(data interface{}, stu interface{}) error {
decodedMap[iter.Key().String()] = iter.Value().Interface()
}

bindTag(decodedMap, stu)
return nil
return bindTag(decodedMap, stu)
}

0 comments on commit 7048ef8

Please sign in to comment.