snow/person/attribute.go

59 lines
1.0 KiB
Go
Raw Normal View History

2023-11-01 00:05:17 +00:00
package person
import (
"encoding/json"
"reflect"
2023-11-01 00:05:17 +00:00
"github.com/ectrc/snow/storage"
"github.com/google/uuid"
2023-11-01 00:05:17 +00:00
)
type Attribute struct {
ID string
2023-11-01 00:05:17 +00:00
Key string
Value interface{}
Type string
}
func NewAttribute(key string, value interface{}) *Attribute {
2023-11-01 00:05:17 +00:00
return &Attribute{
ID: uuid.New().String(),
2023-11-01 00:05:17 +00:00
Key: key,
Value: value,
Type: reflect.TypeOf(value).String(),
2023-11-01 00:05:17 +00:00
}
}
func FromDatabaseAttribute(db *storage.DB_PAttribute) *Attribute {
var value interface{}
err := json.Unmarshal([]byte(db.ValueJSON), &value)
if err != nil {
return nil
}
return &Attribute{
ID: db.ID,
2023-11-01 00:05:17 +00:00
Key: db.Key,
Value: value,
Type: db.Type,
}
}
func (a *Attribute) ToDatabase(profileId string) *storage.DB_PAttribute {
value, err := json.Marshal(a.Value)
if err != nil {
return nil
}
return &storage.DB_PAttribute{
ID: a.ID,
2023-11-01 00:05:17 +00:00
ProfileID: profileId,
Key: a.Key,
2023-11-01 00:05:17 +00:00
ValueJSON: string(value),
Type: a.Type,
2023-11-01 00:05:17 +00:00
}
}
func (a *Attribute) Delete() {
storage.Repo.DeleteAttribute(a.ID)
2023-11-01 00:05:17 +00:00
}