Skip to content

Commit

Permalink
smile decode to json compliant of numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
saurav-zomato committed Mar 24, 2022
1 parent 06ef4fe commit a71b730
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

.idea
13 changes: 12 additions & 1 deletion decode/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (d *Decoder) parseBinaryLongTextStructureValues(smileBytes []byte) ([]byte,
return smileBytes, object, err
}

object[key.(string)] = value
object[key.(string)] = toJsonCompliant(value)
}
return smileBytes[1:], object, nil
case LONG_VARIABLE_ASCII:
Expand Down Expand Up @@ -111,3 +111,14 @@ func (d *Decoder) parseBinaryLongTextStructureValues(smileBytes []byte) ([]byte,

return nil, nil, fmt.Errorf("unknown byte '%X' in parseBinaryLongTextStructureValues\n", nextByte)
}

func toJsonCompliant(val interface{}) interface{} {
switch val.(type) {
case int:
return float64(val.(int))
case float32:
return float64(val.(float32))
default:
return val
}
}
4 changes: 4 additions & 0 deletions smile/smile.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func DecodeToObject(smile []byte) (interface{}, error) {
return "", err
}

if _, err := domain.DecodeHeader(smile[header.SizeBytes:]); err == nil {
smile = smile[header.SizeBytes:]
}

var d decode.Decoder
_, b, err := d.DecodeBytes(smile[header.SizeBytes:])
return b, err
Expand Down
17 changes: 17 additions & 0 deletions test/testdata/solr-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"q":"*:*",
"fl":"score, id",
"wt":"smile"
}},
"response":{"numFound":2,"start":0,"maxScore":1.0,"numFoundExact":true,"docs":[
{
"id":"1",
"score":1.0},
{
"id":"2",
"score":1.0}]
}}
Binary file added test/testdata/solr-response.smile
Binary file not shown.

0 comments on commit a71b730

Please sign in to comment.