and.go 933 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package approve_former
  2. import "strings"
  3. type And struct {
  4. childNodes []Noder
  5. }
  6. func NewAnd(childNodes ...Noder) *And {
  7. return &And{
  8. childNodes: childNodes,
  9. }
  10. }
  11. func (and *And) GetWhen() string {
  12. var when string
  13. for _, childNode := range and.childNodes {
  14. if when == "" {
  15. when = "(" + childNode.GetWhen() + ")"
  16. } else {
  17. when = when + " && " + "(" + childNode.GetWhen() + ")"
  18. }
  19. }
  20. return when
  21. }
  22. func (and *And) AddNodes(childNodes ...Noder) *And {
  23. if childNodes == nil || len(childNodes) == 0 {
  24. and.childNodes = append(and.childNodes, childNodes...)
  25. }
  26. return and
  27. }
  28. func (and *And) Render(upperNode Noder) (string, error) {
  29. var content string
  30. for _, childNode := range and.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. }