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.
 
 
 

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