You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.9 KiB

  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. package document
  15. import "fmt"
  16. type Document struct {
  17. ID string `json:"id"`
  18. Fields []Field `json:"fields"`
  19. CompositeFields []*CompositeField
  20. Number uint64 `json:"-"`
  21. }
  22. func NewDocument(id string) *Document {
  23. return &Document{
  24. ID: id,
  25. Fields: make([]Field, 0),
  26. CompositeFields: make([]*CompositeField, 0),
  27. }
  28. }
  29. func (d *Document) AddField(f Field) *Document {
  30. switch f := f.(type) {
  31. case *CompositeField:
  32. d.CompositeFields = append(d.CompositeFields, f)
  33. default:
  34. d.Fields = append(d.Fields, f)
  35. }
  36. return d
  37. }
  38. func (d *Document) GoString() string {
  39. fields := ""
  40. for i, field := range d.Fields {
  41. if i != 0 {
  42. fields += ", "
  43. }
  44. fields += fmt.Sprintf("%#v", field)
  45. }
  46. compositeFields := ""
  47. for i, field := range d.CompositeFields {
  48. if i != 0 {
  49. compositeFields += ", "
  50. }
  51. compositeFields += fmt.Sprintf("%#v", field)
  52. }
  53. return fmt.Sprintf("&document.Document{ID:%s, Fields: %s, CompositeFields: %s}", d.ID, fields, compositeFields)
  54. }
  55. func (d *Document) NumPlainTextBytes() uint64 {
  56. rv := uint64(0)
  57. for _, field := range d.Fields {
  58. rv += field.NumPlainTextBytes()
  59. }
  60. for _, compositeField := range d.CompositeFields {
  61. for _, field := range d.Fields {
  62. if compositeField.includesField(field.Name()) {
  63. rv += field.NumPlainTextBytes()
  64. }
  65. }
  66. }
  67. return rv
  68. }