| package streaming |
|
|
| import ( |
| "errors" |
| "slices" |
|
|
| "github.com/openmeterio/openmeter/pkg/models" |
| ) |
|
|
| |
| type Customer interface { |
| GetUsageAttribution() CustomerUsageAttribution |
| } |
|
|
| |
| func NewCustomerUsageAttribution(id string, key *string, subjectKeys []string) CustomerUsageAttribution { |
| customerUsageAttribution := CustomerUsageAttribution{ |
| ID: id, |
| Key: key, |
| SubjectKeys: subjectKeys, |
| } |
|
|
| if customerUsageAttribution.SubjectKeys == nil { |
| customerUsageAttribution.SubjectKeys = []string{} |
| } |
|
|
| return customerUsageAttribution |
| } |
|
|
| |
| type CustomerUsageAttribution struct { |
| |
| ID string `json:"id"` |
| |
| Key *string `json:"key"` |
| |
| SubjectKeys []string `json:"subjectKeys"` |
| } |
|
|
| |
| func (ua CustomerUsageAttribution) Validate() error { |
| if ua.ID == "" { |
| return models.NewGenericValidationError(errors.New("usage attribution must have an id")) |
| } |
|
|
| if ua.Key == nil && len(ua.SubjectKeys) == 0 { |
| return models.NewGenericValidationError(errors.New("usage attribution must have a key or subject keys")) |
| } |
|
|
| for _, subjectKey := range ua.SubjectKeys { |
| if subjectKey == "" { |
| return models.NewGenericValidationError(errors.New("subject key cannot be empty")) |
| } |
| } |
|
|
| return nil |
| } |
|
|
| |
| func (ua CustomerUsageAttribution) GetValues() []string { |
| attributions := []string{} |
|
|
| if ua.Key != nil { |
| attributions = append(attributions, *ua.Key) |
| } |
|
|
| attributions = append(attributions, ua.SubjectKeys...) |
|
|
| return attributions |
| } |
|
|
| |
| func (ua CustomerUsageAttribution) Equal(other CustomerUsageAttribution) bool { |
| if ua.ID != other.ID { |
| return false |
| } |
|
|
| |
| if (ua.Key == nil) != (other.Key == nil) { |
| return false |
| } |
| if ua.Key != nil && *ua.Key != *other.Key { |
| return false |
| } |
|
|
| return slices.Equal(ua.SubjectKeys, other.SubjectKeys) |
| } |
|
|