Skip to content

Commit

Permalink
Fill _score field with 0.0 if _score is missing in response
Browse files Browse the repository at this point in the history
GitHub: #31
  • Loading branch information
s-yata committed Feb 18, 2016
1 parent 0cf2180 commit 5a39b76
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
49 changes: 37 additions & 12 deletions grnci.go
Original file line number Diff line number Diff line change
Expand Up @@ -1124,20 +1124,45 @@ func (db *DB) selectParse(data []byte, vals interface{}, fields []*FieldInfo) (i
rawCols := raw[0][1]
nCols := len(rawCols)
if nCols != len(fields) {
return 0, fmt.Errorf("%d columns expected but %d columns actual",
len(fields), nCols)
// Remove _score from fields if _score does not exist in the response.
for i, field := range fields {
if field.ColumnName() == "_score" {
hasScore := false
for _, rawCol := range rawCols {
var nameType []string
if err := json.Unmarshal(rawCol, &nameType); err != nil {
return 0, err
}
if nameType[0] == "_score" {
hasScore = true
break
}
}
if !hasScore {
for j := i + 1; j < len(fields); j++ {
fields[j-1] = fields[j]
}
fields = fields[:len(fields)-1]
}
break
}
}
if nCols != len(fields) {
return 0, fmt.Errorf("%d columns expected but %d columns actual",
len(fields), nCols)
}
}
// FIXME: the following check disallows functions.
// for i, rawCol := range rawCols {
// var nameType []string
// if err := json.Unmarshal(rawCol, &nameType); err != nil {
// return 0, err
// }
// if nameType[0] != fields[i].ColumnName() {
// return 0, fmt.Errorf("column %#v expected but column %#v actual",
// fields[i].ColumnName(), nameType[0])
// }
// }
// for i, rawCol := range rawCols {
// var nameType []string
// if err := json.Unmarshal(rawCol, &nameType); err != nil {
// return 0, err
// }
// if nameType[0] != fields[i].ColumnName() {
// return 0, fmt.Errorf("column %#v expected but column %#v actual",
// fields[i].ColumnName(), nameType[0])
// }
// }

rawRecs := raw[0][2:]
nRecs := len(rawRecs)
Expand Down
9 changes: 5 additions & 4 deletions grnci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,11 @@ func TestSelect(t *testing.T) {
}

type tblRec2 struct {
Key Text `grnci:"_key;;TABLE_PAT_KEY"`
Bool Bool `grnci:"!bool"`
Int Int `grnci:"int+2;Int32"`
Float Float `grnci:"float*2.0"`
Key Text `grnci:"_key;;TABLE_PAT_KEY"`
Bool Bool `grnci:"!bool"`
Int Int `grnci:"int+2;Int32"`
Float Float `grnci:"float*2.0"`
Score Float `grnci:"_score"`
}

options := NewSelectOptions()
Expand Down

0 comments on commit 5a39b76

Please sign in to comment.