[Groonga-commit] groonga/grnci at 5a39b76 [master] Fill _score field with 0.0 if _score is missing in response

Back to archive index

Susumu Yata null+****@clear*****
Thu Feb 18 16:01:08 JST 2016


Susumu Yata	2016-02-18 16:01:08 +0900 (Thu, 18 Feb 2016)

  New Revision: 5a39b761f1381de27cf6ad1558c3224e4a7e772a
  https://github.com/groonga/grnci/commit/5a39b761f1381de27cf6ad1558c3224e4a7e772a

  Message:
    Fill _score field with 0.0 if _score is missing in response
    
    GitHub: #31

  Modified files:
    grnci.go
    grnci_test.go

  Modified: grnci.go (+37 -12)
===================================================================
--- grnci.go    2016-02-18 15:45:01 +0900 (2a5f758)
+++ grnci.go    2016-02-18 16:01:08 +0900 (72f2a6d)
@@ -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)

  Modified: grnci_test.go (+5 -4)
===================================================================
--- grnci_test.go    2016-02-18 15:45:01 +0900 (8be21ae)
+++ grnci_test.go    2016-02-18 16:01:08 +0900 (b7664dc)
@@ -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()
-------------- next part --------------
HTML����������������������������...
Descargar 



More information about the Groonga-commit mailing list
Back to archive index