1// Copyright (c) 2014 Couchbase, Inc. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package document 16 17import "fmt" 18 19type Document struct { 20 ID string `json:"id"` 21 Fields []Field `json:"fields"` 22 CompositeFields []*CompositeField 23} 24 25func NewDocument(id string) *Document { 26 return &Document{ 27 ID: id, 28 Fields: make([]Field, 0), 29 CompositeFields: make([]*CompositeField, 0), 30 } 31} 32 33func (d *Document) AddField(f Field) *Document { 34 switch f := f.(type) { 35 case *CompositeField: 36 d.CompositeFields = append(d.CompositeFields, f) 37 default: 38 d.Fields = append(d.Fields, f) 39 } 40 return d 41} 42 43func (d *Document) GoString() string { 44 fields := "" 45 for i, field := range d.Fields { 46 if i != 0 { 47 fields += ", " 48 } 49 fields += fmt.Sprintf("%#v", field) 50 } 51 compositeFields := "" 52 for i, field := range d.CompositeFields { 53 if i != 0 { 54 compositeFields += ", " 55 } 56 compositeFields += fmt.Sprintf("%#v", field) 57 } 58 return fmt.Sprintf("&document.Document{ID:%s, Fields: %s, CompositeFields: %s}", d.ID, fields, compositeFields) 59} 60 61func (d *Document) NumPlainTextBytes() uint64 { 62 rv := uint64(0) 63 for _, field := range d.Fields { 64 rv += field.NumPlainTextBytes() 65 } 66 for _, compositeField := range d.CompositeFields { 67 for _, field := range d.Fields { 68 if compositeField.includesField(field.Name()) { 69 rv += field.NumPlainTextBytes() 70 } 71 } 72 } 73 return rv 74} 75