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.
 
 
 

176 lines
4.7 KiB

  1. // Copyright 2017 Google LLC
  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 firestore
  15. import (
  16. "errors"
  17. "fmt"
  18. "time"
  19. "github.com/golang/protobuf/ptypes"
  20. pb "google.golang.org/genproto/googleapis/firestore/v1"
  21. )
  22. // A Precondition modifies a Firestore update or delete operation.
  23. type Precondition interface {
  24. // Returns the corresponding Precondition proto.
  25. preconditionProto() (*pb.Precondition, error)
  26. }
  27. // Exists is a Precondition that checks for the existence of a resource before
  28. // writing to it. If the check fails, the write does not occur.
  29. var Exists Precondition
  30. func init() {
  31. // Initialize here so godoc doesn't show the internal value.
  32. Exists = exists(true)
  33. }
  34. type exists bool
  35. func (e exists) preconditionProto() (*pb.Precondition, error) {
  36. return &pb.Precondition{
  37. ConditionType: &pb.Precondition_Exists{bool(e)},
  38. }, nil
  39. }
  40. func (e exists) String() string {
  41. if e {
  42. return "Exists"
  43. }
  44. return "DoesNotExist"
  45. }
  46. // LastUpdateTime returns a Precondition that checks that a resource must exist and
  47. // must have last been updated at the given time. If the check fails, the write
  48. // does not occur.
  49. func LastUpdateTime(t time.Time) Precondition { return lastUpdateTime(t) }
  50. type lastUpdateTime time.Time
  51. func (u lastUpdateTime) preconditionProto() (*pb.Precondition, error) {
  52. ts, err := ptypes.TimestampProto(time.Time(u))
  53. if err != nil {
  54. return nil, err
  55. }
  56. return &pb.Precondition{
  57. ConditionType: &pb.Precondition_UpdateTime{ts},
  58. }, nil
  59. }
  60. func (u lastUpdateTime) String() string { return fmt.Sprintf("LastUpdateTime(%s)", time.Time(u)) }
  61. func processPreconditionsForDelete(preconds []Precondition) (*pb.Precondition, error) {
  62. // At most one option permitted.
  63. switch len(preconds) {
  64. case 0:
  65. return nil, nil
  66. case 1:
  67. return preconds[0].preconditionProto()
  68. default:
  69. return nil, fmt.Errorf("firestore: conflicting preconditions: %+v", preconds)
  70. }
  71. }
  72. func processPreconditionsForUpdate(preconds []Precondition) (*pb.Precondition, error) {
  73. // At most one option permitted, and it cannot be Exists.
  74. switch len(preconds) {
  75. case 0:
  76. // If the user doesn't provide any options, default to Exists(true).
  77. return exists(true).preconditionProto()
  78. case 1:
  79. if _, ok := preconds[0].(exists); ok {
  80. return nil, errors.New("cannot use Exists with Update")
  81. }
  82. return preconds[0].preconditionProto()
  83. default:
  84. return nil, fmt.Errorf("firestore: conflicting preconditions: %+v", preconds)
  85. }
  86. }
  87. func processPreconditionsForVerify(preconds []Precondition) (*pb.Precondition, error) {
  88. // At most one option permitted.
  89. switch len(preconds) {
  90. case 0:
  91. return nil, nil
  92. case 1:
  93. return preconds[0].preconditionProto()
  94. default:
  95. return nil, fmt.Errorf("firestore: conflicting preconditions: %+v", preconds)
  96. }
  97. }
  98. // A SetOption modifies a Firestore set operation.
  99. type SetOption interface {
  100. fieldPaths() (fps []FieldPath, all bool, err error)
  101. }
  102. // MergeAll is a SetOption that causes all the field paths given in the data argument
  103. // to Set to be overwritten. It is not supported for struct data.
  104. var MergeAll SetOption = merge{all: true}
  105. // Merge returns a SetOption that causes only the given field paths to be
  106. // overwritten. Other fields on the existing document will be untouched. It is an
  107. // error if a provided field path does not refer to a value in the data passed to
  108. // Set.
  109. func Merge(fps ...FieldPath) SetOption {
  110. for _, fp := range fps {
  111. if err := fp.validate(); err != nil {
  112. return merge{err: err}
  113. }
  114. }
  115. return merge{paths: fps}
  116. }
  117. type merge struct {
  118. all bool
  119. paths []FieldPath
  120. err error
  121. }
  122. func (m merge) String() string {
  123. if m.err != nil {
  124. return fmt.Sprintf("<Merge error: %v>", m.err)
  125. }
  126. if m.all {
  127. return "MergeAll"
  128. }
  129. return fmt.Sprintf("Merge(%+v)", m.paths)
  130. }
  131. func (m merge) fieldPaths() (fps []FieldPath, all bool, err error) {
  132. if m.err != nil {
  133. return nil, false, m.err
  134. }
  135. if err := checkNoDupOrPrefix(m.paths); err != nil {
  136. return nil, false, err
  137. }
  138. if m.all {
  139. return nil, true, nil
  140. }
  141. return m.paths, false, nil
  142. }
  143. func processSetOptions(opts []SetOption) (fps []FieldPath, all bool, err error) {
  144. switch len(opts) {
  145. case 0:
  146. return nil, false, nil
  147. case 1:
  148. return opts[0].fieldPaths()
  149. default:
  150. return nil, false, fmt.Errorf("conflicting options: %+v", opts)
  151. }
  152. }