github.com/yongjacky/phoenix-go-orm-builder@v0.3.5/cond_if.go (about)

     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  
     5  package builder
     6  
     7  type condIf struct {
     8  	condition bool
     9  	condTrue  Cond
    10  	condFalse Cond
    11  }
    12  
    13  var _ Cond = condIf{}
    14  
    15  // If returns Cond via condition
    16  func If(condition bool, condTrue Cond, condFalse ...Cond) Cond {
    17  	var c = condIf{
    18  		condition: condition,
    19  		condTrue:  condTrue,
    20  	}
    21  	if len(condFalse) > 0 {
    22  		c.condFalse = condFalse[0]
    23  	}
    24  	return c
    25  }
    26  
    27  func (condIf condIf) WriteTo(w Writer) error {
    28  	if condIf.condition {
    29  		return condIf.condTrue.WriteTo(w)
    30  	} else if condIf.condFalse != nil {
    31  		return condIf.condFalse.WriteTo(w)
    32  	}
    33  	return nil
    34  }
    35  
    36  func (condIf condIf) And(conds ...Cond) Cond {
    37  	return And(condIf, And(conds...))
    38  }
    39  
    40  func (condIf condIf) Or(conds ...Cond) Cond {
    41  	return Or(condIf, Or(conds...))
    42  }
    43  
    44  func (condIf condIf) IsValid() bool {
    45  	if condIf.condition {
    46  		return condIf.condTrue != nil
    47  	}
    48  	return condIf.condFalse != nil
    49  }