expressionOutputStream.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package govaluate
  2. import (
  3. "bytes"
  4. )
  5. /*
  6. Holds a series of "transactions" which represent each token as it is output by an outputter (such as ToSQLQuery()).
  7. Some outputs (such as SQL) require a function call or non-c-like syntax to represent an expression.
  8. To accomplish this, this struct keeps track of each translated token as it is output, and can return and rollback those transactions.
  9. */
  10. type expressionOutputStream struct {
  11. transactions []string
  12. }
  13. func (this *expressionOutputStream) add(transaction string) {
  14. this.transactions = append(this.transactions, transaction)
  15. }
  16. func (this *expressionOutputStream) rollback() string {
  17. index := len(this.transactions) - 1
  18. ret := this.transactions[index]
  19. this.transactions = this.transactions[:index]
  20. return ret
  21. }
  22. func (this *expressionOutputStream) createString(delimiter string) string {
  23. var retBuffer bytes.Buffer
  24. var transaction string
  25. penultimate := len(this.transactions) - 1
  26. for i := 0; i < penultimate; i++ {
  27. transaction = this.transactions[i]
  28. retBuffer.WriteString(transaction)
  29. retBuffer.WriteString(delimiter)
  30. }
  31. retBuffer.WriteString(this.transactions[penultimate])
  32. return retBuffer.String()
  33. }