Files
OliveTin/service/internal/stringvariables/entities.go
James Read ff31abe66c
Some checks failed
Build Snapshot / build-snapshot (push) Waiting to run
DevSkim / DevSkim (push) Waiting to run
Buf CI / buf (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Codestyle checks / codestyle (push) Has been cancelled
refactor: Project directories (#541)
2025-03-22 01:06:59 +00:00

60 lines
1.1 KiB
Go

package stringvariables
import (
"fmt"
"regexp"
"strconv"
"strings"
// log "github.com/sirupsen/logrus"
)
var r *regexp.Regexp
func init() {
r = regexp.MustCompile(`{{ *?([a-zA-Z0-9_]+)\.([a-zA-Z0-9_\.]+) *?}}`)
}
func ReplaceEntityVars(prefix string, source string) string {
matches := r.FindAllStringSubmatch(source, -1)
for _, matches := range matches {
if len(matches) == 3 {
property := matches[2]
val := Get(prefix + "." + property)
source = strings.Replace(source, matches[0], val, 1)
}
}
return source
}
func GetEntities(entityTitle string) []string {
var ret []string
count := GetEntityCount(entityTitle)
for i := 0; i < count; i++ {
prefix := GetEntityPrefix(entityTitle, i)
ret = append(ret, prefix)
}
return ret
}
func GetEntityPrefix(entityTitle string, entityIndex int) string {
return "entities." + entityTitle + "." + fmt.Sprintf("%v", entityIndex)
}
func GetEntityCount(entityTitle string) int {
count, _ := strconv.Atoi(Get("entities." + entityTitle + ".count"))
return count
}
func SetEntityCount(entityTitle string, count int) {
Set("entities."+entityTitle+".count", fmt.Sprintf("%v", count))
}