diff --git a/attribute.go b/attribute.go index 1f5b0e5..1020635 100644 --- a/attribute.go +++ b/attribute.go @@ -2,6 +2,7 @@ package acmelib import ( "fmt" + "strings" "time" ) @@ -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 @@ -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 @@ -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. @@ -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 } @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/entity.go b/entity.go index a0779f7..74dc4ee 100644 --- a/entity.go +++ b/entity.go @@ -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) } } diff --git a/message.go b/message.go index 5696a24..26974d4 100644 --- a/message.go +++ b/message.go @@ -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 @@ -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