You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
I was trying to use mapstructure to convert between a protobuf type and a corresponding internal type. The two times had different forms of timestamp where the protobuf type used the well known Timestamp type and the internal type used a time.Time.
My decode hook looked like the following:
var (
tsType = reflect.TypeOf((*types.Timestamp)(nil))
timePtrType = reflect.TypeOf((*time.Time)(nil))
timeType = timePtrType.Elem()
mapStrInf = reflect.TypeOf((map[string]interface{})(nil))
)
// HookTimeToPBtimestamp is a mapstructure decode hook to translate a time.Time value to
// a protobuf Timestamp value.
func HookTimeToPBTimestamp(from, to reflect.Type, data interface{}) (interface{}, error) {
// Note that mapstructure doesn't do direct struct to struct conversion in this case. I
// still don't completely understand why converting the PB TS to time.Time does but
// I suspect it has something to do with the struct containing a concrete time.Time
// as opposed to a pointer to a time.Time. Regardless this path through mapstructure
// first will decode the concrete time.Time into a map[string]interface{} before
// eventually decoding that map[string]interface{} into the *types.Timestamp. One
// other note is that mapstructure ends up creating a new Value and sets it it to
// the time.Time value and thats what gets passed to us. That is why we end up
// seeing a *time.Time instead of a time.Time.
if from == timePtrType && to == mapStrInf {
ts := data.(*time.Time)
nanos := ts.UnixNano()
seconds := nanos / 1000000000
nanos = nanos % 1000000000
return &map[string]interface{}{
"Seconds": seconds,
"Nanos": int32(nanos),
}, nil
}
return data, nil
}
I had a test to ensure my decode hook was working but it was panicing like so:
I managed to find the cause which exists in a couple places. In both the intermediate map being created for the intermediate steps in a struct -> struct decode are not being created in a way that they are settable:
I have a PR almost ready that fixes the issues by creating these maps in a way that they are settable. I just wanted to have an issue open to track when adding my test to mapstructure_bug_test.go.
The text was updated successfully, but these errors were encountered:
I was trying to use mapstructure to convert between a protobuf type and a corresponding internal type. The two times had different forms of timestamp where the protobuf type used the well known Timestamp type and the internal type used a
time.Time
.My decode hook looked like the following:
I had a test to ensure my decode hook was working but it was panicing like so:
I managed to find the cause which exists in a couple places. In both the intermediate map being created for the intermediate steps in a struct -> struct decode are not being created in a way that they are settable:
mapstructure/mapstructure.go
Line 907 in d16e948
mapstructure/mapstructure.go
Lines 1157 to 1158 in d16e948
I have a PR almost ready that fixes the issues by creating these maps in a way that they are settable. I just wanted to have an issue open to track when adding my test to mapstructure_bug_test.go.
The text was updated successfully, but these errors were encountered: