Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated propertyobject #1

Merged
merged 1 commit into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions config/interfaces.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
91 changes: 91 additions & 0 deletions object/propertyobject.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 21 additions & 0 deletions object/serializable.go
Original file line number Diff line number Diff line change
@@ -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
}