Skip to content

Commit

Permalink
String() method improved
Browse files Browse the repository at this point in the history
  • Loading branch information
FerroO2000 committed Apr 12, 2024
1 parent dc4c8e4 commit e158e38
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
85 changes: 85 additions & 0 deletions attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package acmelib

import (
"fmt"
"strings"
"time"
)

Expand All @@ -18,6 +19,11 @@ 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))
}

// Attribute returns the [Attribute] of the [AttributeValue].
func (av *AttributeValue) Attribute() Attribute {
return av.attribute
Expand Down Expand Up @@ -59,6 +65,10 @@ func newAttributeRef(entID EntityID, kind AttributeRefKind, val any) *AttributeR
}
}

func (af *AttributeRef) stringify(b *strings.Builder, tabs int) {
b.WriteString(fmt.Sprintf("%skind: %s; entity_id: %s;value: %v\n", getTabString(tabs), af.kind, af.entityID, af.value))
}

// EntityID returns the entity id of the [AttributeRef]
func (af *AttributeRef) EntityID() EntityID {
return af.entityID
Expand Down Expand Up @@ -109,6 +119,8 @@ type Attribute interface {
// References returns a slice of references of an attribute.
References() []*AttributeRef

String() string

// ToString converts the attribute to a string attribute.
ToString() (*StringAttribute, error)
// ToInteger converts the attribute to a integer attribute.
Expand Down Expand Up @@ -137,6 +149,22 @@ func newAttribute(name, desc string, kind AttributeKind) *attribute {
}
}

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))

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

b.WriteString(fmt.Sprintf("%sreferences:\n", tabStr))
for _, ref := range a.References() {
ref.stringify(b, tabs+1)
}
}

func (a *attribute) Kind() AttributeKind {
return a.kind
}
Expand Down Expand Up @@ -170,6 +198,17 @@ func NewStringAttribute(name, desc, defValue string) *StringAttribute {
}
}

func (sa *StringAttribute) stringify(b *strings.Builder, tabs int) {
sa.attribute.stringify(b, tabs)
b.WriteString(fmt.Sprintf("%sdefault_value: %s\n", getTabString(tabs), sa.defValue))
}

func (sa *StringAttribute) String() string {
builder := new(strings.Builder)
sa.stringify(builder, 0)
return builder.String()
}

// DefValue returns the default value of the [StringAttribute].
func (sa *StringAttribute) DefValue() string {
return sa.defValue
Expand Down Expand Up @@ -230,6 +269,20 @@ func NewIntegerAttribute(name, desc string, defValue, min, max int) (*IntegerAtt
}, nil
}

func (ia *IntegerAttribute) stringify(b *strings.Builder, tabs int) {
ia.attribute.stringify(b, tabs)

tabStr := getTabString(tabs)
b.WriteString(fmt.Sprintf("%smin: %d; max: %d; hex_format: %t\n", tabStr, ia.min, ia.max, ia.isHexFormat))
b.WriteString(fmt.Sprintf("%sdefault_value: %d\n", tabStr, ia.defValue))
}

func (ia *IntegerAttribute) String() string {
builder := new(strings.Builder)
ia.stringify(builder, 0)
return builder.String()
}

// DefValue returns the default value of the [IntegerAttribute].
func (ia *IntegerAttribute) DefValue() int {
return ia.defValue
Expand Down Expand Up @@ -306,6 +359,20 @@ func NewFloatAttribute(name, desc string, defValue, min, max float64) (*FloatAtt
}, nil
}

func (fa *FloatAttribute) stringify(b *strings.Builder, tabs int) {
fa.attribute.stringify(b, tabs)

tabStr := getTabString(tabs)
b.WriteString(fmt.Sprintf("%smin: %g; max: %g\n", tabStr, fa.min, fa.max))
b.WriteString(fmt.Sprintf("%sdefault_value: %g\n", tabStr, fa.defValue))
}

func (fa *FloatAttribute) String() string {
builder := new(strings.Builder)
fa.stringify(builder, 0)
return builder.String()
}

// DefValue returns the default value of the [FloatAttribute].
func (fa *FloatAttribute) DefValue() float64 {
return fa.defValue
Expand Down Expand Up @@ -376,6 +443,24 @@ func NewEnumAttribute(name, desc string, values ...string) (*EnumAttribute, erro
}, nil
}

func (ea *EnumAttribute) stringify(b *strings.Builder, tabs int) {
ea.attribute.stringify(b, tabs)

tabStr := getTabString(tabs)

for idx, val := range ea.Values() {
b.WriteString(fmt.Sprintf("%value: %s; index: %d\n", tabStr, val, idx))
}

b.WriteString(fmt.Sprintf("%sdefault_value: %s\n", tabStr, ea.defValue))
}

func (ea *EnumAttribute) String() string {
builder := new(strings.Builder)
ea.stringify(builder, 0)
return builder.String()
}

// DefValue returns the default value of the [EnumAttribute].
func (ea *EnumAttribute) DefValue() string {
return ea.defValue
Expand Down
3 changes: 1 addition & 2 deletions entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ func (ae *attributeEntity) stringify(b *strings.Builder, tabs int) {
tabStr := getTabString(tabs)
b.WriteString(fmt.Sprintf("%sattribute values:\n", tabStr))
for _, attVal := range ae.AttributeValues() {
b.WriteString(fmt.Sprintf("%s\t%s: %v", tabStr, attVal.attribute.Name(), attVal.value))
b.WriteRune('\n')
attVal.stringify(b, tabs+1)
}
}

Expand Down
22 changes: 21 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
// By default 11 bit message ids are used.
type MessageID uint32

func (mid MessageID) String() string {
return fmt.Sprintf("%d", mid)
}

// MessageIDGeneratorFn is callback used for generating automatically
// the [MessageID] of a [Message]. It is triggered when a [Message] is added to
// a [Node] or when the former is removed. It takes as prameters the priority
Expand Down Expand Up @@ -199,7 +203,23 @@ func (m *Message) stringify(b *strings.Builder, tabs int) {

tabStr := getTabString(tabs)

b.WriteString(fmt.Sprintf("%ssize: %d\n", tabStr, m.sizeByte))
if m.id != 0 {
b.WriteString(fmt.Sprintf("%smessage_id: %d; is_static_id: %t\n", tabStr, m.id, m.isStaticID))
}

b.WriteString(fmt.Sprintf("%spriority: %d (very_high=0; low=3)\n", tabStr, m.priority))
b.WriteString(fmt.Sprintf("%ssize: %d bytes\n", tabStr, m.sizeByte))

if m.cycleTime != 0 {
b.WriteString(fmt.Sprintf("%scycle_time: %d ms\n", tabStr, m.cycleTime))
}

if m.receivers.size() > 0 {
b.WriteString(fmt.Sprintf("%sreceivers:\n", tabStr))
for _, rec := range m.Receivers() {
b.WriteString(fmt.Sprintf("%sname: %s; node_id: %d; entity_id: %s\n", tabStr, rec.name, rec.id, rec.entityID))
}
}

if m.signals.size() == 0 {
return
Expand Down

0 comments on commit e158e38

Please sign in to comment.