-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support for binary marshaler/unmarshaler #17
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4361963
wip
elee1766 cb8365c
add test. change order
elee1766 e1718e1
merge
elee1766 4f070ea
move fields functions to new file
elee1766 4ea7cae
convert
elee1766 c91d72a
add comment, move
elee1766 3fec555
a
elee1766 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package gtrsconvert | ||
|
||
import ( | ||
"encoding" | ||
"reflect" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// ConvertibleTo is implemented by types that can convert themselves to a map. | ||
type ConvertibleTo interface { | ||
ToMap() (map[string]any, error) | ||
} | ||
|
||
// ConvertibleFrom is implemented by types that can load themselves from a map. | ||
type ConvertibleFrom interface { | ||
FromMap(map[string]any) error | ||
} | ||
|
||
// structToMap convert a struct to a map. | ||
func StructToMap(st any) (map[string]any, error) { | ||
if c, ok := st.(ConvertibleTo); ok { | ||
return c.ToMap() | ||
} | ||
|
||
rv := reflect.ValueOf(st) | ||
rt := reflect.TypeOf(st) | ||
out := make(map[string]interface{}, rv.NumField()) | ||
|
||
for i := 0; i < rv.NumField(); i++ { | ||
fieldValue := rv.Field(i) | ||
fieldType := rt.Field(i) | ||
fieldName := getFieldNameFromType(fieldType) | ||
switch v := fieldValue.Interface().(type) { | ||
case time.Time: | ||
out[fieldName] = v.Format(time.RFC3339Nano) | ||
case time.Duration: | ||
out[fieldName] = v.String() | ||
case encoding.TextMarshaler: | ||
txt, err := v.MarshalText() | ||
if err != nil { | ||
return nil, SerializeError{ | ||
Field: fieldType.Name, | ||
Value: fieldValue.Interface(), | ||
Err: err, | ||
} | ||
} | ||
out[fieldName] = string(txt) | ||
default: | ||
out[fieldName] = fieldValue.Interface() | ||
} | ||
} | ||
return out, nil | ||
} | ||
|
||
// mapToStruct tries to convert a map to a struct. | ||
func MapToStruct(st any, data map[string]any) error { | ||
rv := reflect.ValueOf(st).Elem() | ||
rt := reflect.TypeOf(st) | ||
|
||
if rt.Implements(typeOf[ConvertibleFrom]()) { | ||
if c, ok := st.(ConvertibleFrom); ok { | ||
return c.FromMap(data) | ||
} | ||
} | ||
|
||
rt = rt.Elem() | ||
|
||
for i := 0; i < rt.NumField(); i += 1 { | ||
fieldRv := rv.Field(i) | ||
fieldRt := rt.Field(i) | ||
|
||
v, ok := data[getFieldNameFromType(fieldRt)] | ||
if !ok { | ||
continue | ||
} | ||
|
||
// The redis client always sends strings. | ||
stval, ok := v.(string) | ||
if !ok { | ||
continue | ||
} | ||
|
||
val, err := valueFromString(fieldRv, stval) | ||
if err != nil { | ||
return FieldParseError{Field: fieldRt.Name, Value: v, Err: err} | ||
} else { | ||
fieldRv.Set(reflect.ValueOf(val)) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Parse value from string | ||
// TODO: find a better solution. Maybe there is a library for this. | ||
func valueFromString(val reflect.Value, st string) (any, error) { | ||
iface := val.Interface() | ||
|
||
switch cast := iface.(type) { | ||
case string: | ||
return st, nil | ||
case bool: | ||
return strconv.ParseBool(st) | ||
case int: | ||
v, err := strconv.ParseInt(st, 10, 0) | ||
return int(v), err | ||
case uint: | ||
v, err := strconv.ParseUint(st, 10, 0) | ||
return uint(v), err | ||
case int32: | ||
v, err := strconv.ParseInt(st, 10, 32) | ||
return int32(v), err | ||
case uint32: | ||
v, err := strconv.ParseUint(st, 10, 32) | ||
return uint32(v), err | ||
case int64: | ||
return strconv.ParseInt(st, 10, 64) | ||
case uint64: | ||
return strconv.ParseUint(st, 10, 64) | ||
case float32: | ||
v, err := strconv.ParseFloat(st, 32) | ||
return float32(v), err | ||
case float64: | ||
return strconv.ParseFloat(st, 64) | ||
case time.Time: | ||
return time.Parse(time.RFC3339Nano, st) | ||
case time.Duration: | ||
return time.ParseDuration(st) | ||
case encoding.TextUnmarshaler: | ||
return cast, cast.UnmarshalText([]byte(st)) | ||
default: | ||
ifaceptr := val.Addr().Interface() | ||
unMarshaler, ok := ifaceptr.(encoding.TextUnmarshaler) | ||
if ok { | ||
err := unMarshaler.UnmarshalText([]byte(st)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res := reflect.ValueOf(unMarshaler).Elem().Interface() | ||
return res, nil | ||
} | ||
} | ||
|
||
return nil, ErrUnsupportedFieldType | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on what I found in the
go-redis
library it uses onlyBinary[Un]Marshaller
interfaces. On one side we'd allow text types to be send, on the other if a struct implements both Text/Binary, we'd prioritize text whereas the redis client would've used binary 🤔 Not sure... what do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
didn't realize the credit client used binary - I think we should use the same.