or.go 917 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package approve_former
  2. import "strings"
  3. type Or struct {
  4. childNodes []Noder
  5. }
  6. func NewOr(childNodes ...Noder) *Or {
  7. return &Or{
  8. childNodes: childNodes,
  9. }
  10. }
  11. func (or *Or) GetWhen() string {
  12. var when string
  13. for _, childNode := range or.childNodes {
  14. if when == "" {
  15. when = "(" + childNode.GetWhen() + ")"
  16. } else {
  17. when = when + " || " + "(" + childNode.GetWhen() + ")"
  18. }
  19. }
  20. return when
  21. }
  22. func (or *Or) AddNodes(childNodes ...Noder) *Or {
  23. if childNodes == nil || len(childNodes) == 0 {
  24. or.childNodes = append(or.childNodes, childNodes...)
  25. }
  26. return or
  27. }
  28. func (or *Or) Render(upperNode Noder) (string, error) {
  29. var content string
  30. for _, childNode := range or.childNodes {
  31. childNodeContent, err := childNode.Render(upperNode)
  32. if err != nil {
  33. return "", err
  34. }
  35. content = content + childNodeContent + "\n"
  36. }
  37. content = strings.TrimSuffix(content, "\n")
  38. return content, nil
  39. }