Files

74 lines
1.2 KiB
Go
Raw Permalink Normal View History

2025-01-21 18:24:14 +00:00
package client
import (
"fmt"
"regexp"
"strings"
2025-03-05 12:05:46 +00:00
"queryorchestration/internal/serviceconfig"
2025-01-21 18:24:14 +00:00
)
type Client struct {
ID string
Name string
CanSync bool
2025-01-21 18:24:14 +00:00
}
2025-06-03 13:52:10 +00:00
func (c *Client) NormalizeCanSyncUpdate(canSync **bool) {
if canSync == nil || *canSync == nil {
return
2025-01-21 18:24:14 +00:00
}
2025-06-03 13:52:10 +00:00
if c.CanSync == **canSync {
*canSync = nil
}
2025-01-21 18:24:14 +00:00
}
2025-06-03 13:52:10 +00:00
func (c *Client) NormalizeNameUpdate(name **string) error {
if name == nil || *name == nil {
return nil
}
2025-06-03 13:52:10 +00:00
err := normalizeName(*name)
if err != nil {
return err
}
2025-06-03 13:52:10 +00:00
if c.Name == **name {
*name = nil
2025-01-21 18:24:14 +00:00
}
return nil
2025-01-21 18:24:14 +00:00
}
func normalizeName(name *string) error {
if name == nil {
return nil
}
2025-06-03 13:52:10 +00:00
normalizedName := strings.TrimSpace(*name)
spaceRegex := regexp.MustCompile(`\s+`)
normalizedName = spaceRegex.ReplaceAllString(normalizedName, " ")
2025-01-21 18:24:14 +00:00
2025-06-03 13:52:10 +00:00
validNameRegex := `^[a-zA-Z0-9\_\- \(\)\.\,]+$`
2025-01-21 18:24:14 +00:00
2025-06-03 13:52:10 +00:00
alphanumeric := regexp.MustCompile(validNameRegex)
if !alphanumeric.MatchString(normalizedName) {
return fmt.Errorf(`"%s" must have regex: %s`, normalizedName, validNameRegex)
2025-01-21 18:24:14 +00:00
}
2025-06-03 13:52:10 +00:00
*name = normalizedName
return nil
2025-01-21 18:24:14 +00:00
}
type Service struct {
cfg serviceconfig.ConfigProvider
2025-01-21 18:24:14 +00:00
}
func New(cfg serviceconfig.ConfigProvider) *Service {
2025-01-21 18:24:14 +00:00
return &Service{
cfg,
2025-01-21 18:24:14 +00:00
}
}