File size: 5,354 Bytes
fea99b3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | package models
import (
"errors"
"fmt"
"time"
"github.com/openmeterio/openmeter/pkg/clock"
"github.com/openmeterio/openmeter/pkg/equal"
)
type ManagedUniqueResource struct {
NamespacedModel `json:",inline" mapstructure:",squash"`
ManagedModel `json:",inline" mapstructure:",squash"`
// ID is the unique identifier for Resource.
ID string `json:"id"`
// Key is the unique key for Resource.
Key string `json:"key"`
}
type ManagedResource struct {
NamespacedModel `json:",inline" mapstructure:",squash"`
ManagedModel `json:",inline" mapstructure:",squash"`
// ID is the unique identifier for Resource.
ID string `json:"id"`
Description *string `json:"description,omitempty"`
Name string `json:"name"`
}
func (m ManagedResource) GetID() string {
return m.ID
}
func (m ManagedResource) GetDescription() *string {
return m.Description
}
func (m ManagedResource) GetName() string {
return m.Name
}
type ManagedResourceInput struct {
ID string
Namespace string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
Name string
Description *string
}
func (r ManagedResourceInput) Validate() error {
if r.ID == "" {
return errors.New("id is required")
}
if r.Namespace == "" {
return errors.New("namespace is required")
}
if r.CreatedAt.IsZero() {
return errors.New("created at is required")
}
if r.UpdatedAt.IsZero() {
return errors.New("updated at is required")
}
return nil
}
func NewManagedResource(input ManagedResourceInput) ManagedResource {
return ManagedResource{
ID: input.ID,
NamespacedModel: NamespacedModel{
Namespace: input.Namespace,
},
ManagedModel: ManagedModel{
CreatedAt: input.CreatedAt.UTC(),
UpdatedAt: input.UpdatedAt.UTC(),
DeletedAt: func() *time.Time {
if input.DeletedAt == nil {
return nil
}
deletedAt := input.DeletedAt.UTC()
return &deletedAt
}(),
},
Name: input.Name,
Description: input.Description,
}
}
func (r ManagedResource) Validate() error {
if err := r.NamespacedModel.Validate(); err != nil {
return fmt.Errorf("error validating namespaced model: %w", err)
}
if err := r.ManagedModel.Validate(); err != nil {
return fmt.Errorf("error validating managed model: %w", err)
}
if r.ID == "" {
return errors.New("id is required")
}
if r.Name == "" {
return errors.New("name is required")
}
return nil
}
type ManagedModel struct {
CreatedAt time.Time `json:"createdAt"`
// After creation the entity is considered updated.
UpdatedAt time.Time `json:"updatedAt"`
// Time of soft delete. If not null, the entity is considered deleted.
DeletedAt *time.Time `json:"deletedAt,omitempty"`
}
func (m ManagedModel) Validate() error {
if m.CreatedAt.IsZero() {
return errors.New("created at is required")
}
if m.UpdatedAt.IsZero() {
return errors.New("updated at is required")
}
return nil
}
func (m ManagedModel) IsDeleted() bool {
return m.IsDeletedAt(clock.Now())
}
func (m ManagedModel) IsDeletedAt(t time.Time) bool {
if m.DeletedAt == nil {
return false
}
return !m.DeletedAt.After(t)
}
func (m ManagedModel) Equal(other ManagedModel) bool {
if !m.CreatedAt.Equal(other.CreatedAt) {
return false
}
if !m.UpdatedAt.Equal(other.UpdatedAt) {
return false
}
if !equal.PtrEqual(m.DeletedAt, other.DeletedAt) {
return false
}
return true
}
func (m ManagedModel) GetCreatedAt() time.Time {
return m.CreatedAt
}
func (m ManagedModel) GetUpdatedAt() time.Time {
return m.UpdatedAt
}
func (m ManagedModel) GetDeletedAt() *time.Time {
return m.DeletedAt
}
type ManagedModelWithID struct {
ManagedModel `json:",inline" mapstructure:",squash"`
ID string `json:"id"`
}
func (m ManagedModelWithID) GetID() string {
return m.ID
}
func (m ManagedModelWithID) Equal(other ManagedModelWithID) bool {
if m.ID != other.ID {
return false
}
return m.ManagedModel.Equal(other.ManagedModel)
}
func (m ManagedModelWithID) Validate() error {
if m.ID == "" {
return errors.New("id is required")
}
return nil
}
type NamespacedModel struct {
Namespace string `json:"namespace"`
}
func (m NamespacedModel) Validate() error {
if m.Namespace == "" {
return errors.New("namespace is required")
}
return nil
}
func (m NamespacedModel) GetNamespace() string {
return m.Namespace
}
type Address struct {
Country *CountryCode `json:"country,omitempty"`
PostalCode *string `json:"postalCode,omitempty"`
State *string `json:"state,omitempty"`
City *string `json:"city,omitempty"`
Line1 *string `json:"line1,omitempty"`
Line2 *string `json:"line2,omitempty"`
PhoneNumber *string `json:"phoneNumber,omitempty"`
}
// [ISO 3166-1](https://www.iso.org/iso-3166-country-codes.html) alpha-2 country code.
type CountryCode string
// VersionedModel represents a versionable entity. With each new version the sequence is incremented
// while the key remains the same. Key + Version uniquely identifies an entity.
type VersionedModel struct {
// Key is the unique identifier of the entity across its versions.
// Might be generated by the system or provided by the user.
Key string `json:"key,omitempty"`
// Version is the integer sequential version of the entity, starting from 1.
Version int `json:"version,omitempty"`
}
|