| package subscription |
|
|
| import ( |
| "fmt" |
| "time" |
|
|
| "github.com/samber/lo" |
|
|
| "github.com/openmeterio/openmeter/pkg/models" |
| ) |
|
|
| |
| |
|
|
| type ApplyContext struct { |
| CurrentTime time.Time |
| } |
|
|
| |
| type AppliesToSpec interface { |
| |
| |
| ApplyTo(spec *SubscriptionSpec, actx ApplyContext) error |
| } |
|
|
| const ( |
| |
| subscriptionPatchErrAttrNameAllowedDuringApplyingToSpecError = "allowed_during_applying_to_spec_error" |
| ) |
|
|
| func AllowedDuringApplyingToSpecError() models.ValidationIssueOption { |
| return models.WithAttribute(subscriptionPatchErrAttrNameAllowedDuringApplyingToSpecError, true) |
| } |
|
|
| func NewAppliesToSpec(fn func(spec *SubscriptionSpec, actx ApplyContext) error) AppliesToSpec { |
| return &someAppliesToSpec{ |
| Fn: fn, |
| } |
| } |
|
|
| var _ AppliesToSpec = &someAppliesToSpec{} |
|
|
| type someAppliesToSpec struct { |
| Fn func(spec *SubscriptionSpec, actx ApplyContext) error |
| } |
|
|
| func (s *someAppliesToSpec) ApplyTo(spec *SubscriptionSpec, actx ApplyContext) error { |
| return s.Fn(spec, actx) |
| } |
|
|
| |
| func NewAggregateAppliesToSpec(applieses []AppliesToSpec) AppliesToSpec { |
| return NewAppliesToSpec(func(spec *SubscriptionSpec, actx ApplyContext) error { |
| for i, applies := range applieses { |
| if err := spec.Apply(applies, actx); err != nil { |
| wrapError := func(err error) error { |
| return models.ErrorWithComponent(models.ComponentName(fmt.Sprintf("patch at idx %d", i)), err) |
| } |
|
|
| issues, err := models.AsValidationIssues(err) |
| if err != nil { |
| return wrapError(err) |
| } |
|
|
| if lo.EveryBy(issues, func(issue models.ValidationIssue) bool { |
| return IsValidationIssueWithBoolAttr(issue, subscriptionPatchErrAttrNameAllowedDuringApplyingToSpecError) |
| }) { |
| continue |
| } |
|
|
| |
| return wrapError(err) |
| } |
| } |
|
|
| return nil |
| }) |
| } |
|
|