and.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package approve_former
  2. import "strings"
  3. type And struct {
  4. childNodes []Node
  5. }
  6. func NewAnd(childNodes ...Node) *And {
  7. return &And{
  8. childNodes: childNodes,
  9. }
  10. }
  11. func (and *And) GetDepends() string {
  12. var depends string
  13. for _, childNode := range and.childNodes {
  14. if depends == "" {
  15. depends = childNode.GetDepends()
  16. } else {
  17. depends = depends + " && " + childNode.GetDepends()
  18. }
  19. }
  20. return depends
  21. }
  22. func (and *And) GetWhen() string {
  23. var when string
  24. for _, childNode := range and.childNodes {
  25. if when == "" {
  26. when = "(" + childNode.GetWhen() + ")"
  27. } else {
  28. when = when + " && " + "(" + childNode.GetWhen() + ")"
  29. }
  30. }
  31. return when
  32. }
  33. func (and *And) AddNodes(childNodes ...Node) *And {
  34. if childNodes == nil || len(childNodes) == 0 {
  35. and.childNodes = append(and.childNodes, childNodes...)
  36. }
  37. return and
  38. }
  39. func (and *And) Render(upperNode Node) (string, error) {
  40. var content string
  41. for _, childNode := range and.childNodes {
  42. childNodeContent, err := childNode.Render(upperNode)
  43. if err != nil {
  44. return "", err
  45. }
  46. content = content + childNodeContent + "\n"
  47. }
  48. content = strings.TrimSuffix(content, "\n")
  49. return content, nil
  50. }