-
Notifications
You must be signed in to change notification settings - Fork 47
implement wrapper type for custom message param json marshalling #821
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
Conversation
2aefe2c
to
7c579a0
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #821 +/- ##
======================================
Coverage 31.8% 31.8%
======================================
Files 39 39
Lines 3777 3777
======================================
Hits 1202 1202
Misses 2429 2429
Partials 146 146 🚀 New features to boost your workflow:
|
7c579a0
to
0e110db
Compare
0e110db
to
16fa758
Compare
16fa758
to
4da66a7
Compare
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.
While I think this would work it's feels like it's more complex than it needs to be.
How about this alternate approach. Assuming that all message params are simple structs then you could use reflection to convert the param struct to an equivalent map (like is done on line 380 in this PR). If the value of a param field is a bitfield then you wrap it in a struct with its own custom marshaler. Then you just marshal the map.
This approach relies on message params being simple structs without nested maps of other structs, which is valid as far as I can tell.
lens/util/repo.go
Outdated
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.
It would be a lot clearer if you made this a first class function:
type marshaler func(interface{}) ([]byte, error)
func MarshalWithOverrides(v interface{}, overrides map[reflect.Type]marshaler) []byte, error {
...
}
The function then uses paramWraperType as an internal implementation detail.
The marshaler returns an error so you can replace the panics with error returns in your bitfield marshaler.
Then you can call it like this:
var bitfieldCountMarshaler = func(v interface{}) ([]byte, error) {
...
}
b, err := MarshalWithOverrides(b, map[reflect.Type]marshaler{
reflect.TypeOf(bitfield.BitField{}): bitfieldCountMarshaler,
})
e04ef2a
to
af4b87e
Compare
ef9fa60
to
8608a12
Compare
8608a12
to
93aae7e
Compare
var ret = struct { | ||
Count uint64 | ||
RLE []uint64 | ||
}{} |
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.
Given:
Old:
{"Recoveries": [{"Sectors": {"rle": "[10816,31,1407,1,2,23,1,4,1,1,1405,1,3,26,1,1,1,1,521,1,3,915,58,1,2,1343]", "_type": "bitfield", "elemcount": "2349"}, "Deadline": 6, "Partition": 0}]}
I would have made this struct match the existing schema:
var ret = struct {
Count uint64 `json:elemcount`
RLE []uint64 `json:rle`
Type string `json:_type`
}{ Type: "bitfield" }
fixes #819