/
attribute.go
58 lines (46 loc) · 1.53 KB
/
attribute.go
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
package manager
import (
"errors"
"fmt"
"github.com/eko/authz/backend/internal/entity/model"
"github.com/eko/authz/backend/internal/entity/repository"
"gorm.io/gorm"
)
type AttributeRepository repository.Base[model.Attribute]
type Attribute interface {
MapToSlice(attributes map[string]any) ([]*model.Attribute, error)
GetRepository() AttributeRepository
}
type attributeManager struct {
repository AttributeRepository
}
// NewAttribute initializes a new attribute manager.
func NewAttribute(repository AttributeRepository) Attribute {
return &attributeManager{
repository: repository,
}
}
func (m *attributeManager) GetRepository() AttributeRepository {
return m.repository
}
func (m *attributeManager) MapToSlice(attributes map[string]any) ([]*model.Attribute, error) {
var attributeObjects = make([]*model.Attribute, 0)
for attributeKey, attributeValue := range attributes {
value, err := CastAnyToString(attributeValue)
if err != nil {
return nil, fmt.Errorf("unable to cast attribute value to string: %v", err)
}
attribute, err := m.repository.GetByFields(map[string]repository.FieldValue{
"key_name": {Operator: "=", Value: attributeKey},
"value": {Operator: "=", Value: value},
})
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fmt.Errorf("unable to check for existing attribute: %v", err)
}
if attribute == nil {
attribute = &model.Attribute{Key: attributeKey, Value: value}
}
attributeObjects = append(attributeObjects, attribute)
}
return attributeObjects, nil
}