cond_if.go 1004 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2019 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package builder
  5. type condIf struct {
  6. condition bool
  7. condTrue Cond
  8. condFalse Cond
  9. }
  10. var _ Cond = condIf{}
  11. // If returns Cond via condition
  12. func If(condition bool, condTrue Cond, condFalse ...Cond) Cond {
  13. var c = condIf{
  14. condition: condition,
  15. condTrue: condTrue,
  16. }
  17. if len(condFalse) > 0 {
  18. c.condFalse = condFalse[0]
  19. }
  20. return c
  21. }
  22. func (condIf condIf) WriteTo(w Writer) error {
  23. if condIf.condition {
  24. return condIf.condTrue.WriteTo(w)
  25. } else if condIf.condFalse != nil {
  26. return condIf.condFalse.WriteTo(w)
  27. }
  28. return nil
  29. }
  30. func (condIf condIf) And(conds ...Cond) Cond {
  31. return And(condIf, And(conds...))
  32. }
  33. func (condIf condIf) Or(conds ...Cond) Cond {
  34. return Or(condIf, Or(conds...))
  35. }
  36. func (condIf condIf) IsValid() bool {
  37. if condIf.condition {
  38. return condIf.condTrue != nil
  39. }
  40. return condIf.condFalse != nil
  41. }