Merged in feature/update (pull request #27)
Query Update * baselineupdate * baseupdateplusmodelupdates * passtests * somemoresubmittesting * testinnerfunctions * readmeandinstall * cleanerstartup * readmeplusdeps * tidyatrighttime * validatetests * normalizedontvalidate * abitofzenormalizationcleanup * addunitstestforhelperfuns * normalizeactiveversiontestas
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"queryorchestration/internal/database"
|
||||
queryprocessor "queryorchestration/internal/query/processor"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Config interface {
|
||||
GetConfig() *string
|
||||
SetConfig(*string)
|
||||
}
|
||||
|
||||
func (s *Service) normalizeConfig(config Config) error {
|
||||
if config == nil || config.GetConfig() == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
trim := strings.TrimSpace(*config.GetConfig())
|
||||
|
||||
if trim == "" {
|
||||
config.SetConfig(nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
if !json.Valid([]byte(trim)) {
|
||||
return errors.New("invalid config JSON")
|
||||
}
|
||||
|
||||
var data map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(trim), &data); err != nil {
|
||||
return fmt.Errorf("error unmarshalling JSON: %s", err)
|
||||
}
|
||||
|
||||
prettyJSON, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error marshalling JSON: %s", err)
|
||||
}
|
||||
|
||||
strJSON := string(prettyJSON)
|
||||
|
||||
if strJSON == "{}" {
|
||||
config.SetConfig(nil)
|
||||
} else {
|
||||
config.SetConfig(&strJSON)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type RequiredQueryIDs interface {
|
||||
GetRequiredQueryIDs() *[]uuid.UUID
|
||||
SetRequiredQueryIDs(*[]uuid.UUID)
|
||||
}
|
||||
|
||||
func (s *Service) normalizeQueryIDs(ctx context.Context, ids RequiredQueryIDs) error {
|
||||
if ids == nil || ids.GetRequiredQueryIDs() == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
ide := ids.GetRequiredQueryIDs()
|
||||
|
||||
if len(*ide) == 0 {
|
||||
ids.SetRequiredQueryIDs(nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
dbids := database.MustToDBUUIDArray(*ide)
|
||||
|
||||
exist, err := s.db.Queries.AllQueriesExist(ctx, dbids)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !exist {
|
||||
return errors.New("not all required ids are present")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) normalizeActiveVersion(current *Query, entity *queryprocessor.Update) error {
|
||||
if current == nil {
|
||||
return errors.New("current query required")
|
||||
}
|
||||
|
||||
if entity == nil || entity.ActiveVersion == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if entity.ActiveVersion == ¤t.ActiveVersion {
|
||||
entity.ActiveVersion = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
if *entity.ActiveVersion < 1 || *entity.ActiveVersion > current.LatestVersion+1 {
|
||||
return fmt.Errorf("active version must be in the range: 1 <= activeVersion <= %d", current.LatestVersion+1)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user