range.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright 2016 CoreOS, 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 recipe
  15. import (
  16. v3 "github.com/coreos/etcd/clientv3"
  17. )
  18. func withFirstCreate() []v3.OpOption { return withTop(v3.SortByCreatedRev, v3.SortAscend) }
  19. func withLastCreate() []v3.OpOption { return withTop(v3.SortByCreatedRev, v3.SortDescend) }
  20. func withFirstKey() []v3.OpOption { return withTop(v3.SortByKey, v3.SortAscend) }
  21. func withLastKey() []v3.OpOption { return withTop(v3.SortByKey, v3.SortDescend) }
  22. func withFirstRev() []v3.OpOption { return withTop(v3.SortByModifiedRev, v3.SortAscend) }
  23. func withLastRev() []v3.OpOption { return withTop(v3.SortByModifiedRev, v3.SortDescend) }
  24. // withTop gets the first key over the get's prefix given a sort order
  25. func withTop(target v3.SortTarget, order v3.SortOrder) []v3.OpOption {
  26. return []v3.OpOption{
  27. v3.WithPrefix(),
  28. v3.WithSort(target, order),
  29. v3.WithLimit(1)}
  30. }