diff options
-rw-r--r-- | client/go/internal/vespa/document/document.go | 29 | ||||
-rw-r--r-- | client/go/internal/vespa/document/document_test.go | 10 |
2 files changed, 29 insertions, 10 deletions
diff --git a/client/go/internal/vespa/document/document.go b/client/go/internal/vespa/document/document.go index 616013dc59a..0fcdc8610ec 100644 --- a/client/go/internal/vespa/document/document.go +++ b/client/go/internal/vespa/document/document.go @@ -3,6 +3,7 @@ package document import ( "bufio" "bytes" + "errors" "fmt" "io" "math/rand" @@ -36,10 +37,23 @@ const ( ) var ( + ErrMissingId = errors.New("no id specified") fieldsPrefix = []byte(`{"fields":`) fieldsSuffix = []byte("}") ) +func (o Operation) String() string { + switch o { + case OperationPut: + return "put" + case OperationUpdate: + return "update" + case OperationRemove: + return "remove" + } + return "" +} + // Id represents a Vespa document ID. type Id struct { id string @@ -152,14 +166,8 @@ type Decoder struct { func (d Document) String() string { var sb strings.Builder - switch d.Operation { - case OperationPut: - sb.WriteString("put ") - case OperationUpdate: - sb.WriteString("update ") - case OperationRemove: - sb.WriteString("remove ") - } + sb.WriteString(d.Operation.String()) + sb.WriteString(" ") sb.WriteString(d.Id.String()) if d.Condition != "" { sb.WriteString(", condition=") @@ -228,7 +236,7 @@ func (d *Decoder) readBool() (bool, error) { func (d *Decoder) Decode() (Document, error) { doc, err := d.decode() if err != nil && err != io.EOF { - return Document{}, fmt.Errorf("invalid json at byte offset %d: %w", d.dec.InputOffset(), err) + return doc, fmt.Errorf("invalid operation at byte offset %d: %w", d.dec.InputOffset(), err) } return doc, err } @@ -347,6 +355,9 @@ loop: break loop } } + if doc.Id.id == "" { + return doc, ErrMissingId + } return doc, nil } diff --git a/client/go/internal/vespa/document/document_test.go b/client/go/internal/vespa/document/document_test.go index f9bf321f1fb..d37febf3da8 100644 --- a/client/go/internal/vespa/document/document_test.go +++ b/client/go/internal/vespa/document/document_test.go @@ -1,6 +1,7 @@ package document import ( + "errors" "fmt" "io" "strings" @@ -204,10 +205,17 @@ func TestDocumentDecoderInvalid(t *testing.T) { t.Errorf("unexpected error: %s", err) } _, err = dec.Decode() - wantErr := "invalid json at byte offset 110: json: invalid character '\\n' within string (expecting non-control character)" + wantErr := "invalid operation at byte offset 110: json: invalid character '\\n' within string (expecting non-control character)" if err.Error() != wantErr { t.Errorf("want error %q, got %q", wantErr, err.Error()) } + + dec = NewDecoder(strings.NewReader(`{}`)) + _, err = dec.Decode() + wantErr = "invalid operation at byte offset 2: no id specified" + if !errors.Is(err, ErrMissingId) { + t.Errorf("want error %q, got %q", ErrMissingId, err.Error()) + } } func benchmarkDocumentDecoder(b *testing.B, size int) { |