Skip to content

Commit

Permalink
attribute protos
Browse files Browse the repository at this point in the history
  • Loading branch information
FerroO2000 committed May 22, 2024
1 parent 1dea029 commit b8af3ac
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 114 deletions.
157 changes: 68 additions & 89 deletions attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newAttributeValue(att Attribute, val any) *AttributeValue {

func (av *AttributeValue) stringify(b *strings.Builder, tabs int) {
b.WriteString(fmt.Sprintf("%sname: %s; type: %s; value: %v\n",
getTabString(tabs), av.attribute.Name(), av.attribute.Kind(), av.value))
getTabString(tabs), av.attribute.Name(), av.attribute.Type(), av.value))
}

// Attribute returns the [Attribute] of the [AttributeValue].
Expand Down Expand Up @@ -84,20 +84,35 @@ func (af *AttributeRef) Value() any {
return af.value
}

// AttributeKind defines the kind of an [Attribute].
type AttributeKind string
// AttributeType defines the type of an [Attribute].
type AttributeType int

const (
// AttributeKindString defines a string attribute.
AttributeKindString AttributeKind = "attribute-string"
// AttributeKindInteger defines an integer attribute.
AttributeKindInteger AttributeKind = "attribute-integer"
// AttributeKindFloat defines a float attribute.
AttributeKindFloat AttributeKind = "attribute-float"
// AttributeKindEnum defines an enum attribute.
AttributeKindEnum AttributeKind = "attribute-enum"
// AttributeTypeString defines a string attribute.
AttributeTypeString AttributeType = iota
// AttributeTypeInteger defines an integer attribute.
AttributeTypeInteger
// AttributeTypeFloat defines a float attribute.
AttributeTypeFloat
// AttributeTypeEnum defines an enum attribute.
AttributeTypeEnum
)

func (at AttributeType) String() string {
switch at {
case AttributeTypeString:
return "string"
case AttributeTypeInteger:
return "integer"
case AttributeTypeFloat:
return "float"
case AttributeTypeEnum:
return "enum"
default:
return "unknown"
}
}

// Attribute interface specifies all common methods of
// [StringAttribute], [IntegerAttribute], [FloatAttribute], and
// [EnumAttribute].
Expand All @@ -111,8 +126,8 @@ type Attribute interface {
// CreateTime returns the time of creation of an attribute.
CreateTime() time.Time

// Kind returns the kind of an attribute.
Kind() AttributeKind
// Type returns the kind of an attribute.
Type() AttributeType

addReference(ref *AttributeRef)
removeReference(refID EntityID)
Expand All @@ -133,19 +148,17 @@ type Attribute interface {

type attribute struct {
*entity
*withRefs[*AttributeRef]

kind AttributeKind

references *set[EntityID, *AttributeRef]
typ AttributeType
}

func newAttribute(name string, kind AttributeKind) *attribute {
func newAttribute(name string, typ AttributeType) *attribute {
return &attribute{
entity: newEntity(name),

kind: kind,
entity: newEntity(name),
withRefs: newWithRefs[*AttributeRef](),

references: newSet[EntityID, *AttributeRef](),
typ: typ,
}
}

Expand All @@ -162,9 +175,9 @@ func (a *attribute) stringify(b *strings.Builder, tabs int) {
a.entity.stringify(b, tabs)

tabStr := getTabString(tabs)
b.WriteString(fmt.Sprintf("%skind: %s\n", tabStr, a.kind))
b.WriteString(fmt.Sprintf("%skind: %s\n", tabStr, a.typ))

if a.references.size() == 0 {
if a.refs.size() == 0 {
return
}

Expand All @@ -174,20 +187,16 @@ func (a *attribute) stringify(b *strings.Builder, tabs int) {
}
}

func (a *attribute) Kind() AttributeKind {
return a.kind
func (a *attribute) Type() AttributeType {
return a.typ
}

func (a *attribute) addReference(ref *AttributeRef) {
a.references.add(ref.entityID, ref)
a.refs.add(ref.entityID, ref)
}

func (a *attribute) removeReference(refID EntityID) {
a.references.remove(refID)
}

func (a *attribute) References() []*AttributeRef {
return a.references.getValues()
a.refs.remove(refID)
}

// StringAttribute is an [Attribute] that holds a string value.
Expand All @@ -201,7 +210,7 @@ type StringAttribute struct {
// and default value.
func NewStringAttribute(name, defValue string) *StringAttribute {
return &StringAttribute{
attribute: newAttribute(name, AttributeKindString),
attribute: newAttribute(name, AttributeTypeString),

defValue: defValue,
}
Expand Down Expand Up @@ -231,24 +240,24 @@ func (sa *StringAttribute) ToString() (*StringAttribute, error) {
// ToInteger always returns an error.
func (sa *StringAttribute) ToInteger() (*IntegerAttribute, error) {
return nil, sa.errorf(&ConversionError{
From: string(AttributeKindString),
To: string(AttributeKindInteger),
From: AttributeTypeString.String(),
To: AttributeTypeInteger.String(),
})
}

// ToFloat always returns an error.
func (sa *StringAttribute) ToFloat() (*FloatAttribute, error) {
return nil, sa.errorf(&ConversionError{
From: string(AttributeKindString),
To: string(AttributeKindFloat),
From: AttributeTypeString.String(),
To: AttributeTypeFloat.String(),
})
}

// ToEnum always returns an error.
func (sa *StringAttribute) ToEnum() (*EnumAttribute, error) {
return nil, sa.errorf(&ConversionError{
From: string(AttributeKindString),
To: string(AttributeKindEnum),
From: AttributeTypeString.String(),
To: AttributeTypeEnum.String(),
})
}

Expand Down Expand Up @@ -290,7 +299,7 @@ func NewIntegerAttribute(name string, defValue, min, max int) (*IntegerAttribute
}

return &IntegerAttribute{
attribute: newAttribute(name, AttributeKindInteger),
attribute: newAttribute(name, AttributeTypeInteger),

defValue: defValue,
min: min,
Expand Down Expand Up @@ -342,8 +351,8 @@ func (ia *IntegerAttribute) IsHexFormat() bool {
// ToString always returns an error.
func (ia *IntegerAttribute) ToString() (*StringAttribute, error) {
return nil, ia.errorf(&ConversionError{
From: string(AttributeKindInteger),
To: string(AttributeKindString),
From: AttributeTypeInteger.String(),
To: AttributeTypeString.String(),
})
}

Expand All @@ -355,16 +364,16 @@ func (ia *IntegerAttribute) ToInteger() (*IntegerAttribute, error) {
// ToFloat always returns an error.
func (ia *IntegerAttribute) ToFloat() (*FloatAttribute, error) {
return nil, ia.errorf(&ConversionError{
From: string(AttributeKindInteger),
To: string(AttributeKindFloat),
From: AttributeTypeInteger.String(),
To: AttributeTypeFloat.String(),
})
}

// ToEnum always returns an error.
func (ia *IntegerAttribute) ToEnum() (*EnumAttribute, error) {
return nil, ia.errorf(&ConversionError{
From: string(AttributeKindInteger),
To: string(AttributeKindEnum),
From: AttributeTypeInteger.String(),
To: AttributeTypeEnum.String(),
})
}

Expand Down Expand Up @@ -404,7 +413,7 @@ func NewFloatAttribute(name string, defValue, min, max float64) (*FloatAttribute
}

return &FloatAttribute{
attribute: newAttribute(name, AttributeKindFloat),
attribute: newAttribute(name, AttributeTypeFloat),

defValue: defValue,
min: min,
Expand Down Expand Up @@ -444,16 +453,16 @@ func (fa *FloatAttribute) Max() float64 {
// ToString always returns an error.
func (fa *FloatAttribute) ToString() (*StringAttribute, error) {
return nil, fa.errorf(&ConversionError{
From: string(AttributeKindFloat),
To: string(AttributeKindString),
From: AttributeTypeFloat.String(),
To: AttributeTypeString.String(),
})
}

// ToInteger always returns an error.
func (fa *FloatAttribute) ToInteger() (*IntegerAttribute, error) {
return nil, fa.errorf(&ConversionError{
From: string(AttributeKindFloat),
To: string(AttributeKindInteger),
From: AttributeTypeFloat.String(),
To: AttributeTypeInteger.String(),
})
}

Expand All @@ -465,8 +474,8 @@ func (fa *FloatAttribute) ToFloat() (*FloatAttribute, error) {
// ToEnum always returns an error.
func (fa *FloatAttribute) ToEnum() (*EnumAttribute, error) {
return nil, fa.errorf(&ConversionError{
From: string(AttributeKindFloat),
To: string(AttributeKindEnum),
From: AttributeTypeFloat.String(),
To: AttributeTypeEnum.String(),
})
}

Expand Down Expand Up @@ -501,7 +510,7 @@ func NewEnumAttribute(name string, values ...string) (*EnumAttribute, error) {
}

return &EnumAttribute{
attribute: newAttribute(name, AttributeKindEnum),
attribute: newAttribute(name, AttributeTypeEnum),

defValue: values[0],
values: valSet,
Expand Down Expand Up @@ -568,58 +577,28 @@ func (ea *EnumAttribute) GetValueAtIndex(valueIndex int) (string, error) {
// ToString always returns an error.
func (ea *EnumAttribute) ToString() (*StringAttribute, error) {
return nil, ea.errorf(&ConversionError{
From: string(AttributeKindEnum),
To: string(AttributeKindString),
From: AttributeTypeEnum.String(),
To: AttributeTypeString.String(),
})
}

// ToInteger always returns an error.
func (ea *EnumAttribute) ToInteger() (*IntegerAttribute, error) {
return nil, ea.errorf(&ConversionError{
From: string(AttributeKindEnum),
To: string(AttributeKindInteger),
From: AttributeTypeEnum.String(),
To: AttributeTypeInteger.String(),
})
}

// ToFloat always returns an error.
func (ea *EnumAttribute) ToFloat() (*FloatAttribute, error) {
return nil, ea.errorf(&ConversionError{
From: string(AttributeKindEnum),
To: string(AttributeKindFloat),
From: AttributeTypeEnum.String(),
To: AttributeTypeFloat.String(),
})
}

// ToEnum returns the [EnumAttribute] itself.
func (ea *EnumAttribute) ToEnum() (*EnumAttribute, error) {
return ea, nil
}

type referenceableEntity interface {
EntityID() EntityID
}

type withRefs[R referenceableEntity] struct {
refs *set[EntityID, R]
}

func newWithRefs[R referenceableEntity]() *withRefs[R] {
return &withRefs[R]{
refs: newSet[EntityID, R](),
}
}

func (t *withRefs[R]) addRef(ref R) {
t.refs.add(ref.EntityID(), ref)
}

func (t *withRefs[R]) removeRef(refID EntityID) {
t.refs.remove(refID)
}

func (t *withRefs[R]) ReferenceCount() int {
return t.refs.size()
}

func (t *withRefs[R]) References() []R {
return t.refs.getValues()
}
8 changes: 4 additions & 4 deletions attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func Test_StringAttribute(t *testing.T) {
assert := assert.New(t)

strAtt := NewStringAttribute("str_att", "def_val")
assert.Equal(AttributeKindString, strAtt.Kind())
assert.Equal(AttributeTypeString, strAtt.Type())
assert.Equal("str_att", strAtt.Name())
assert.Equal("def_val", strAtt.DefValue())

Expand All @@ -30,7 +30,7 @@ func Test_IntegerAttribute(t *testing.T) {

intAtt, err := NewIntegerAttribute("int_att", 10, 0, 100)
assert.NoError(err)
assert.Equal(AttributeKindInteger, intAtt.Kind())
assert.Equal(AttributeTypeInteger, intAtt.Type())
assert.Equal("int_att", intAtt.Name())
assert.Equal(10, intAtt.DefValue())
assert.Equal(0, intAtt.Min())
Expand Down Expand Up @@ -60,7 +60,7 @@ func Test_FloatAttribute(t *testing.T) {

floatAtt, err := NewFloatAttribute("float_att", 10, 0, 100)
assert.NoError(err)
assert.Equal(AttributeKindFloat, floatAtt.Kind())
assert.Equal(AttributeTypeFloat, floatAtt.Type())
assert.Equal("float_att", floatAtt.Name())
assert.Equal(10.0, floatAtt.DefValue())
assert.Equal(0.0, floatAtt.Min())
Expand Down Expand Up @@ -90,7 +90,7 @@ func Test_EnumAttribute(t *testing.T) {

enumAtt, err := NewEnumAttribute("enum_att", "val_0", "val_1", "val_2")
assert.NoError(err)
assert.Equal(AttributeKindEnum, enumAtt.Kind())
assert.Equal(AttributeTypeEnum, enumAtt.Type())
assert.Equal("enum_att", enumAtt.Name())
assert.Equal("val_0", enumAtt.DefValue())
assert.Equal(3, len(enumAtt.Values()))
Expand Down
Loading

0 comments on commit b8af3ac

Please sign in to comment.