entities.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package stringvariables
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. // log "github.com/sirupsen/logrus"
  8. )
  9. var r *regexp.Regexp
  10. func init() {
  11. r = regexp.MustCompile(`{{ *?([a-zA-Z0-9_]+)\.([a-zA-Z0-9_\.]+) *?}}`)
  12. }
  13. func ReplaceEntityVars(prefix string, source string) string {
  14. matches := r.FindAllStringSubmatch(source, -1)
  15. for _, matches := range matches {
  16. if len(matches) == 3 {
  17. property := matches[2]
  18. val := Get(prefix + "." + property)
  19. source = strings.Replace(source, matches[0], val, 1)
  20. }
  21. }
  22. return source
  23. }
  24. func GetEntities(entityTitle string) []string {
  25. var ret []string
  26. count := GetEntityCount(entityTitle)
  27. for i := 0; i < count; i++ {
  28. prefix := GetEntityPrefix(entityTitle, i)
  29. ret = append(ret, prefix)
  30. }
  31. return ret
  32. }
  33. func GetEntityPrefix(entityTitle string, entityIndex int) string {
  34. return "entities." + entityTitle + "." + fmt.Sprintf("%v", entityIndex)
  35. }
  36. func GetEntityCount(entityTitle string) int {
  37. count, _ := strconv.Atoi(Get("entities." + entityTitle + ".count"))
  38. return count
  39. }
  40. func SetEntityCount(entityTitle string, count int) {
  41. Set("entities."+entityTitle+".count", fmt.Sprintf("%v", count))
  42. }