Comments (9)
I'm struggling with this feature request, but I think it has some merit.
The c/c++ msgpack lib is somewhat complicated, but the MSGPACK_DEFINE macro may be something that may help give a hint to the library, possibly allowing it encode a struct as a map.
However, there is still merit on the Go library to decode to support decoding an encoded array in to a struct. After some thoughts and prototyping it, I'd add support for the feature and let you know when to test it.
Thanks.
from go.
Please test this out and let me know that it worked for you.
from go.
hi, I've test this feature, it works well both with encoded map and encoded array, without any change in my test code, thanks!
however, after reading souce code that you commited, I realized that I also need an option can encode a struct to encoded array, for communation with msgpack cpp lib.
I understand that the cpp lib would be better accept encoded map as struct, but cpp is complicated, so I think better provide this option in golang lib.
comment?
from go.
Hi,
I'm not comfortable with the idea of supporting a feature so that it works as the cpp library works. I'm more comfortable is we can defend the feature on its own merits.
Let me think about it and follow up.
from go.
I thought about this, and the feature has merit on its own.
It allows user to create a non-homogenous but fully typed array in code.
For example, if you expect a 4-element array of integer, bool, string and map[string]int, you can define it using a type like:
type MyRequest struct {
_struct bool `codec:",toarray"`
Num int
Flag bool
Name string
Properties map[string]int
}
Usage model is via one of the following:
type mh codec.MsgpackHandle
mh.StructToArray = true
// ...
Or by defining it in the struct tag:
type Mystruct struct {
_struct bool `codec:",toarray"`
// ...
}
Please test out and let me know if this works for you.
from go.
great! that's all I wanted.
I'll test this feature later.
from go.
FYI: I've fixed the bugs I found while testing. Specifically, we have to use the same consistent logic during encoding and decoding. Please test out and let me know so I can close the issue.
Thanks.
from go.
I've test encode struct as array, it works fine with cpp lib.
thanks for providing these great features!
from go.
Made more robust with change b25a055 .
from go.
Related Issues (20)
- omitempty handling for arrays disagrees with the standard encoding/json HOT 2
- codec.MsgpackHandle.RawToString is confusingly named HOT 2
- Go 1.20 - invalid memory address or nil pointer dereference HOT 5
- Msgpack deserialization with codecgen produces wrong result HOT 3
- Unexpected encoding with interface{} key using Canonical and MapKeyAsString.
- Decoding a slice of structs encoded as arrays into a slice of structs without the same number of fields HOT 2
- Duplicate key inside of a map triggers a "cannot decode unsigned integer" msgpack decode error HOT 1
- Efficient direct conversion between supported formats? HOT 2
- Expose Handle.Name() in some form through Encoder and Decoder for use by codec.Selfer HOT 1
- `io.ErrUnexpectedEOF` should be returned instead of `io.EOF` when only partial data is present
- Add `DecodeOptions.IntType` and `DecodeOptions.FloatType` ? HOT 1
- Docs do not seem to mention case-sensitive field handling
- mem leak? HOT 1
- Tests use rand.Intn(1) for boolean rand, but always returns 0
- Decoder accepts indefinite-length CBOR strings containing chunks with mismatched major types HOT 1
- UTF-8 validation isn't applied to CBOR text strings nested inside an indefinite-length text string HOT 2
- Add support for github.com/go-json-experiment/json / UnmarshalerV2 ? HOT 1
- runtime error with golang master (furture go 1.22)
- Leading zeros is not allowed in rfc7159
- attr.go
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go.