From 2e5029f73213b5df03603e18a34af84042f081a6 Mon Sep 17 00:00:00 2001 From: lesichkovm Date: Sat, 15 Mar 2025 20:25:15 +0000 Subject: [PATCH] updated propertyobject --- config/interfaces.go | 20 +++++++++ object/propertyobject.go | 91 ++++++++++++++++++++++++++++++++++++++++ object/serializable.go | 21 ++++++++++ 3 files changed, 132 insertions(+) create mode 100644 config/interfaces.go create mode 100644 object/propertyobject.go create mode 100644 object/serializable.go diff --git a/config/interfaces.go b/config/interfaces.go new file mode 100644 index 0000000..b3e65ac --- /dev/null +++ b/config/interfaces.go @@ -0,0 +1,20 @@ +package config + +import "github.com/gouniverse/dataobject" + +// Config represents the application configuration +type ConfigInterface interface { + dataobject.DataObjectInterface +} + +// sonfig represents the application configuration +type configImplementation struct { + dataobject.DataObject +} + +func NewConfig() ConfigInterface { + o := dataobject.NewDataObject() + return &configImplementation{ + DataObject: *o, + } +} diff --git a/object/propertyobject.go b/object/propertyobject.go new file mode 100644 index 0000000..652997d --- /dev/null +++ b/object/propertyobject.go @@ -0,0 +1,91 @@ +package object + +import ( + "encoding/json" + "errors" + + "github.com/google/uuid" +) + +type PropertyObjectInterface interface { + Get(key string) (any, error) + Set(key string, value any) error + // Delete(key string) error + // Has(key string) (bool, error) + // Clear() error + // Count() (int, error) + // Keys() ([]string, error) + // Values() ([]interface{}, error) + // Items() ([][2]interface{}, error) +} + +type PropertyObject struct { + id string + properties map[string]any +} + +func NewPropertyObject() PropertyObjectInterface { + return &PropertyObject{ + id: uuid.New().String(), + properties: make(map[string]any), + } +} + +func (p *PropertyObject) GetID() string { + return p.id +} + +func (p *PropertyObject) SetID(id string) error { + if id == "" { + return errors.New("id cannot be empty") + } + p.id = id + return nil +} + +func (p *PropertyObject) Get(key string) (any, error) { + value, exists := p.properties[key] + if !exists { + return nil, errors.New("property not found") + } + return value, nil +} + +func (p *PropertyObject) Set(key string, value any) error { + p.properties[key] = value + return nil +} + +// ToJSON serializes the PropertyObject to JSON +func (p *PropertyObject) ToJSON() ([]byte, error) { + // Create a map that includes both the ID and properties + data := map[string]interface{}{ + "id": p.id, + "properties": p.properties, + } + return json.Marshal(data) +} + +// FromJSON deserializes JSON data into the PropertyObject +func (p *PropertyObject) FromJSON(data []byte) error { + // Create a temporary structure to hold the deserialized data + temp := struct { + ID string `json:"id"` + Properties map[string]any `json:"properties"` + }{} + + if err := json.Unmarshal(data, &temp); err != nil { + return err + } + + // Update the object with the deserialized data + if temp.ID != "" { + p.id = temp.ID + } + + if temp.Properties != nil { + p.properties = temp.Properties + } + + return nil +} diff --git a/object/serializable.go b/object/serializable.go new file mode 100644 index 0000000..0740d8c --- /dev/null +++ b/object/serializable.go @@ -0,0 +1,21 @@ +package object + +// Serializable defines an interface for objects that can be serialized/deserialized +type SerializableInterface interface { + // GetID returns the unique identifier for this object + GetID() string + + // SetID sets the unique identifier for this object + SetID(id string) error + + // ToJSON serializes the object to JSON + ToJSON() ([]byte, error) + + // FromJSON deserializes JSON data into the object + FromJSON(data []byte) error +} + +type SerializablePropertyObjectInterface interface { + PropertyObjectInterface + SerializableInterface +}