Files
query-orchestration/internal/client/service.go
T
Michael McGuinness ed8cfbbee4 Merged in feature/collectorset (pull request #96)
Set Collector

* set
2025-03-10 13:00:57 +00:00

82 lines
1.1 KiB
Go

package client
import (
"errors"
"fmt"
"regexp"
"strings"
"queryorchestration/internal/serviceconfig"
"github.com/google/uuid"
)
type Client struct {
ID uuid.UUID
Name string
CanSync bool
}
func (c *Client) normalizeCanSyncUpdate(n **bool) {
if n == nil || *n == nil {
return
}
if c.CanSync == **n {
*n = nil
}
}
func (c *Client) normalizeNameUpdate(n **string) error {
if n == nil || *n == nil {
return nil
}
err := normalizeName(*n)
if err != nil {
return err
}
if c.Name == **n {
*n = nil
}
return nil
}
func normalizeName(name *string) error {
if name == nil {
return nil
}
uname := strings.TrimSpace(*name)
if uname == "" {
return errors.New("name required")
}
spacere := regexp.MustCompile(`\s+`)
uname = spacere.ReplaceAllString(uname, " ")
reg := `^[a-zA-Z0-9\_\- ]+$`
alphanumeric := regexp.MustCompile(reg)
if !alphanumeric.MatchString(uname) {
return fmt.Errorf("name must have regex: %s", reg)
}
*name = uname
return nil
}
type Service struct {
cfg serviceconfig.ConfigProvider
}
func New(cfg serviceconfig.ConfigProvider) *Service {
return &Service{
cfg,
}
}