entities.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 RemoveKeysThatStartWith(search string) {
  25. for k, _ := range contents {
  26. if strings.HasPrefix(k, search) {
  27. delete(contents, k)
  28. }
  29. }
  30. }
  31. func GetEntities(entityTitle string) []string {
  32. var ret []string
  33. count := GetEntityCount(entityTitle)
  34. for i := 0; i < count; i++ {
  35. prefix := GetEntityPrefix(entityTitle, i)
  36. ret = append(ret, prefix)
  37. }
  38. return ret
  39. }
  40. func GetEntityPrefix(entityTitle string, entityIndex int) string {
  41. return "entities." + entityTitle + "." + fmt.Sprintf("%v", entityIndex)
  42. }
  43. func GetEntityCount(entityTitle string) int {
  44. count, _ := strconv.Atoi(Get("entities." + entityTitle + ".count"))
  45. return count
  46. }
  47. func SetEntityCount(entityTitle string, count int) {
  48. Set("entities."+entityTitle+".count", fmt.Sprintf("%v", count))
  49. }