Skip to content

Commit

Permalink
feat: add change to interface method(ByteStorage#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
qishenonly committed Jul 7, 2023
1 parent eb10c1e commit 77bac83
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions structure/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,39 @@ const (
)

// interfaceToBytes converts an interface to a byte slice
func interfaceToBytes(value interface{}) ([]byte, error) {
func interfaceToBytes(value interface{}) ([]byte, error, string) {
switch value := value.(type) {

case string:
return []byte(value), nil
return []byte(value), nil, "string"
case int:
return []byte(strconv.Itoa(value)), nil
return []byte(strconv.Itoa(value)), nil, "int"
case int64:
return []byte(strconv.FormatInt(value, 10)), nil
return []byte(strconv.FormatInt(value, 10)), nil, "int64"
case float64:
return []byte(strconv.FormatFloat(value, 'f', -1, 64)), nil
return []byte(strconv.FormatFloat(value, 'f', -1, 64)), nil, "float64"
case bool:
return []byte(strconv.FormatBool(value)), nil
return []byte(strconv.FormatBool(value)), nil, "bool"
case []byte:
return value, nil, "[]byte"
default:
return nil, errors.New("unsupported type"), ""
}
}

func byteToInterface(value []byte, dataType string) (interface{}, error) {
switch dataType {
case "string":
return string(value), nil
case "int":
return strconv.Atoi(string(value))
case "int64":
return strconv.ParseInt(string(value), 10, 64)
case "float64":
return strconv.ParseFloat(string(value), 64)
case "bool":
return strconv.ParseBool(string(value))
case "[]byte":
return value, nil
default:
return nil, errors.New("unsupported type")
Expand Down

0 comments on commit 77bac83

Please sign in to comment.