Skip to content

Commit

Permalink
feat: add features validation
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 23, 2024
1 parent f31413c commit 2055b08
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
1 change: 0 additions & 1 deletion internal/controller/system/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func (c *DefaultController) GetLedgerController(ctx context.Context, name string
func (c *DefaultController) CreateLedger(ctx context.Context, name string, configuration ledger.Configuration) error {
return tracing.SkipResult(tracing.Trace(ctx, "CreateLedger", tracing.NoResult(func(ctx context.Context) error {
configuration.SetDefaults()
// todo: validate queried features
l, err := ledger.New(name, configuration)
if err != nil {
return err
Expand Down
35 changes: 29 additions & 6 deletions internal/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ var (
bucketNameFormat = regexp.MustCompile("^[0-9a-zA-Z_-]{1,63}$")
)

func validateFeatureWithValue(feature, value string) error {
possibleConfigurations, ok := FeatureConfigurations[feature]
if !ok {
return fmt.Errorf("feature %q not exists", feature)
}
if !slices.Contains(possibleConfigurations, value) {
return fmt.Errorf("configuration %s it not possible for feature %s", value, feature)
}

return nil
}

type FeatureSet map[string]string

func (f FeatureSet) With(feature, value string) FeatureSet {
Expand Down Expand Up @@ -90,6 +102,16 @@ func (c *Configuration) SetDefaults() {
}
}

func (c *Configuration) Validate() error {
for feature, value := range c.Features {
if err := validateFeatureWithValue(feature, value); err != nil {
return err
}
}

return nil
}

func NewDefaultConfiguration() Configuration {
return Configuration{
Bucket: DefaultBucket,
Expand All @@ -107,18 +129,19 @@ type Ledger struct {
}

func (l Ledger) HasFeature(feature, value string) bool {
possibleConfigurations, ok := FeatureConfigurations[feature]
if !ok {
panic(fmt.Sprintf("feature %q not exists", feature))
}
if !slices.Contains(possibleConfigurations, value) {
panic(fmt.Sprintf("configuration %s it not possible for feature %s", value, feature))
if err := validateFeatureWithValue(feature, value); err != nil {
panic(err)
}

return l.Features[feature] == value
}

func New(name string, configuration Configuration) (*Ledger, error) {

if err := configuration.Validate(); err != nil {
return nil, err
}

if !ledgerNameFormat.MatchString(name) {
return nil, newErrInvalidLedgerName(name, fmt.Errorf("name must match format '%s'", ledgerNameFormat.String()))
}
Expand Down

0 comments on commit 2055b08

Please sign in to comment.