-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
fix: wrong way of Unmarshal #4397
base: master
Are you sure you want to change the base?
Conversation
@kevwan please help review |
…onx.Marshal but jsonx.Umarshal works wrong way
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files
|
//and also u can find this https://stackoverflow.com/questions/34089750/marshal-byte-to-json-giving-a-strange-string | ||
if fieldType.Elem().Kind() == reflect.Uint8 { | ||
strVal := mapValue.(string) | ||
decodedBytes, err := base64.StdEncoding.DecodeString(strVal) |
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.
How can we know it's a base64 encoded string?
I recommend you to use string type and then decode it to []byte.
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.
First in that switch case we can knwo that only type string and reflect type slice can setp into , and if the reflect element is uint8(byte) it have the probability that the original format is a byte slice ([]byte), and after encoding, it becomes a string.
So I use base64.StdEncoding.DecodeString
try to figure out the []byte.This is better than I wirte a new func to parse string to []byte.
As mentioned above, we have established that valueKind is string. Therefore, within the if statement, I use type assertion to convert the type of value to string. Additionally, if any changes are made inside fillSliceFromString
, I would not be able to determine whether the target type for conversion is []byte. Hence, performing the conversion from string to []byte here is the optimal approach
I faced a minor problem when I used it.
for examlpe:
and
get
returnsIt's ok when I visit
/get
api ,but when I put/get
response (the json data ofrequest
) into/test
error happend:and I notice that ,there use json.Marshal() to make the response but use slef custom mapping.UnmarshalJsonReader to Unmarshal json data
so I just fix it (core in mapping.processFieldNotFromString).
You can see test in TestUnmarshalJsonReaderMultiArray