47fec079e5
Demo prep + Fix client sync * firstversion * clientsync * configlint * fixtests
74 lines
1.0 KiB
Go
74 lines
1.0 KiB
Go
package client
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"queryorchestration/internal/serviceconfig"
|
|
)
|
|
|
|
type Client struct {
|
|
ID string
|
|
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)
|
|
spacere := regexp.MustCompile(`\s+`)
|
|
uname = spacere.ReplaceAllString(uname, " ")
|
|
|
|
reg := `^[a-zA-Z0-9\_\- \(\)\.\,]+$`
|
|
|
|
alphanumeric := regexp.MustCompile(reg)
|
|
if !alphanumeric.MatchString(uname) {
|
|
return fmt.Errorf(`"%s" must have regex: %s`, uname, reg)
|
|
}
|
|
|
|
*name = uname
|
|
|
|
return nil
|
|
}
|
|
|
|
type Service struct {
|
|
cfg serviceconfig.ConfigProvider
|
|
}
|
|
|
|
func New(cfg serviceconfig.ConfigProvider) *Service {
|
|
return &Service{
|
|
cfg,
|
|
}
|
|
}
|