diff --git a/.github/workflows/go_tests.yml b/.github/workflows/go_tests.yml index 6592726..b89a19b 100644 --- a/.github/workflows/go_tests.yml +++ b/.github/workflows/go_tests.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: ['1.21', '1.22'] + go-version: ['1.22'] steps: - uses: actions/checkout@v4 diff --git a/README.md b/README.md index 17c1633..478083b 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,10 @@ The package documentation can be found [here](https://pkg.go.dev/github.com/Ferr ## Getting started +### Prerequisites + +[Golang](https://go.dev/) 1.22 + ### Installation Run the following Go command to install the `acmelib` package: diff --git a/errors.go b/errors.go index b475e4b..abc1271 100644 --- a/errors.go +++ b/errors.go @@ -19,7 +19,7 @@ type ArgumentError struct { } func (e *ArgumentError) Error() string { - return fmt.Sprintf("argument; name:%q : %v", e.Name, e.Err) + return fmt.Sprintf("argument error; name:%q : %v", e.Name, e.Err) } func (e *ArgumentError) Unwrap() error { return e.Err } @@ -31,31 +31,41 @@ type SignalError struct { } func (e *SignalError) Error() string { - return fmt.Sprintf("multiplexer signal; entity_id:%q, name:%q : %v", e.EntityID.String(), e.Name, e.Err) + return fmt.Sprintf("multiplexer signal error; entity_id:%q, name:%q : %v", e.EntityID.String(), e.Name, e.Err) } func (e *SignalError) Unwrap() error { return e.Err } -type SignalNameError struct { +type NameError struct { Name string Err error } -func (e *SignalNameError) Error() string { - return fmt.Sprintf("signal name %q : %v", e.Name, e.Err) +func (e *NameError) Error() string { + return fmt.Sprintf("name error; name:%q : %v", e.Name, e.Err) } -func (e *SignalNameError) Unwrap() error { +func (e *NameError) Unwrap() error { return e.Err } +type UpdateNameError struct { + Err error +} + +func (e *UpdateNameError) Error() string { + return fmt.Sprintf("update name error : %v", e.Err) +} + +func (e *UpdateNameError) Unwrap() error { return e.Err } + type GroupIDError struct { GroupID int Err error } func (e *GroupIDError) Error() string { - return fmt.Sprintf("group id %d : %v", e.GroupID, e.Err) + return fmt.Sprintf("group id error; group_id:%d : %v", e.GroupID, e.Err) } func (e *GroupIDError) Unwrap() error { return e.Err } @@ -68,7 +78,7 @@ type InsertSignalError struct { } func (e *InsertSignalError) Error() string { - return fmt.Sprintf("insert signal; entity_id:%q, name:%q, start_bit:%d : %v", e.EntityID.String(), e.Name, e.StartBit, e.Err) + return fmt.Sprintf("insert signal error; entity_id:%q, name:%q, start_bit:%d : %v", e.EntityID.String(), e.Name, e.StartBit, e.Err) } func (e *InsertSignalError) Unwrap() error { return e.Err } @@ -79,7 +89,7 @@ type RemoveSignalError struct { } func (e *RemoveSignalError) Error() string { - return fmt.Sprintf("remove signal; entity_id:%q : %v", e.EntityID.String(), e.Err) + return fmt.Sprintf("remove signal error; entity_id:%q : %v", e.EntityID.String(), e.Err) } func (e *RemoveSignalError) Unwrap() error { return e.Err } @@ -89,7 +99,7 @@ type ClearSignalGroupError struct { } func (e *ClearSignalGroupError) Error() string { - return fmt.Sprintf("clear signal group : %v", e.Err) + return fmt.Sprintf("clear signal group error : %v", e.Err) } func (e *ClearSignalGroupError) Unwrap() error { return e.Err } diff --git a/go.mod b/go.mod index e75fbc0..cbc54f2 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/FerroO2000/acmelib -go 1.21.4 +go 1.22 require ( github.com/jaevor/go-nanoid v1.3.0 diff --git a/message.go b/message.go index de54505..97837c2 100644 --- a/message.go +++ b/message.go @@ -148,7 +148,7 @@ func (m *Message) GetSignalParentKind() SignalParentKind { return SignalParentKindMessage } -func (m *Message) verifySignalName(_ EntityID, name string) error { +func (m *Message) verifySignalName(name string) error { return m.signalNames.verifyKeyUnique(name) } @@ -274,11 +274,59 @@ func (m *Message) SenderNode() *Node { return m.senderNode } +func (m *Message) addSignal(sig Signal) { + sigID := sig.EntityID() + + m.signals.add(sigID, sig) + m.signalNames.add(sig.Name(), sigID) + + sig.setParentMsg(m) + + if sig.Kind() == SignalKindMultiplexer { + muxSig, err := sig.ToMultiplexer() + if err != nil { + panic(err) + } + + for tmpSigID, tmpSig := range muxSig.signals.entries() { + m.signals.add(tmpSigID, tmpSig) + } + + for tmpName, tmpSigID := range muxSig.signalNames.entries() { + m.signalNames.add(tmpName, tmpSigID) + } + } +} + +func (m *Message) removeSignal(sig Signal) { + sigID := sig.EntityID() + + m.signals.remove(sigID) + m.signalNames.remove(sig.Name()) + + sig.setParentMsg(nil) + + if sig.Kind() == SignalKindMultiplexer { + muxSig, err := sig.ToMultiplexer() + if err != nil { + panic(err) + } + + for _, tmpSigID := range muxSig.signals.getKeys() { + m.signals.remove(tmpSigID) + } + + for _, tmpName := range muxSig.signalNames.getKeys() { + m.signalNames.remove(tmpName) + } + } +} + // AppendSignal appends a [Signal] to the last position of the [Message] payload. // It may return an error if the signal name is already used within the message, // or if the signal cannot fit in the available space left at the end of the message payload. func (m *Message) AppendSignal(signal Signal) error { - if err := m.verifySignalName(signal.EntityID(), signal.Name()); err != nil { + if err := m.verifySignalName(signal.Name()); err != nil { return m.errorf(fmt.Errorf(`cannot append signal "%s" : %w`, signal.Name(), err)) } @@ -286,10 +334,13 @@ func (m *Message) AppendSignal(signal Signal) error { return m.errorf(err) } - m.signals.add(signal.EntityID(), signal) - m.signalNames.add(signal.Name(), signal.EntityID()) + m.addSignal(signal) + + // m.signals.add(signal.EntityID(), signal) + // m.signalNames.add(signal.Name(), signal.EntityID()) - signal.setParent(m) + // signal.setParent(m) + // signal.setParentMsg(m) return nil } @@ -299,7 +350,7 @@ func (m *Message) AppendSignal(signal Signal) error { // It may return an error if the signal name is already used within the message, // or if the signal cannot fit in the available space left at the start bit. func (m *Message) InsertSignal(signal Signal, startBit int) error { - if err := m.verifySignalName(signal.EntityID(), signal.Name()); err != nil { + if err := m.verifySignalName(signal.Name()); err != nil { return m.errorf(fmt.Errorf(`cannot insert signal "%s" : %w`, signal.Name(), err)) } @@ -307,10 +358,13 @@ func (m *Message) InsertSignal(signal Signal, startBit int) error { return m.errorf(err) } - m.signals.add(signal.EntityID(), signal) - m.signalNames.add(signal.Name(), signal.EntityID()) + m.addSignal(signal) - signal.setParent(m) + // m.signals.add(signal.EntityID(), signal) + // m.signalNames.add(signal.Name(), signal.EntityID()) + + // signal.setParent(m) + // signal.setParentMsg(m) return nil } @@ -323,22 +377,25 @@ func (m *Message) RemoveSignal(signalEntityID EntityID) error { return m.errorf(fmt.Errorf(`cannot remove signal with entity id "%s" : %w`, signalEntityID, err)) } - if sig.Kind() == SignalKindMultiplexer { - muxSig, err := sig.ToMultiplexer() - if err != nil { - panic(err) - } + // if sig.Kind() == SignalKindMultiplexer { + // muxSig, err := sig.ToMultiplexer() + // if err != nil { + // panic(err) + // } - for _, tmpSig := range muxSig.signals.entries() { - m.signals.remove(tmpSig.EntityID()) - m.signalNames.remove(tmpSig.Name()) - } - } + // for _, tmpSig := range muxSig.signals.entries() { + // m.signals.remove(tmpSig.EntityID()) + // m.signalNames.remove(tmpSig.Name()) + // } + // } - sig.setParent(nil) + // sig.setParent(nil) + // sig.setParentMsg(nil) - m.signals.remove(signalEntityID) - m.signalNames.remove(sig.Name()) + // m.signals.remove(signalEntityID) + // m.signalNames.remove(sig.Name()) + + m.removeSignal(sig) m.signalPayload.remove(signalEntityID) @@ -348,7 +405,8 @@ func (m *Message) RemoveSignal(signalEntityID EntityID) error { // RemoveAllSignals removes all signals from the [Message]. func (m *Message) RemoveAllSignals() { for _, tmpSig := range m.signals.entries() { - tmpSig.setParent(nil) + // tmpSig.setParent(nil) + tmpSig.setParentMsg(nil) } m.signals.clear() diff --git a/mux_signal.go b/mux_signal.go index 6a1ed44..82daeaa 100644 --- a/mux_signal.go +++ b/mux_signal.go @@ -69,14 +69,6 @@ func NewMultiplexerSignal(name string, groupCount, groupSize int) (*MultiplexerS }, nil } -func (ms *MultiplexerSignal) errorf(err error) error { - return &SignalError{ - EntityID: ms.entityID, - Name: ms.name, - Err: err, - } -} - func (ms *MultiplexerSignal) addSignal(sig Signal) { id := sig.EntityID() name := sig.Name() @@ -84,48 +76,71 @@ func (ms *MultiplexerSignal) addSignal(sig Signal) { ms.signals.add(id, sig) ms.signalNames.add(name, id) - sig.setParent(ms) + // sig.setParent(ms) + sig.setParentMuxSig(ms) - if ms.hasParent() { - parent := ms.Parent() - for parent != nil { - switch parent.GetSignalParentKind() { - case SignalParentKindMultiplexerSignal: - muxParent, err := parent.ToParentMultiplexerSignal() - if err != nil { - panic(err) - } - parent = muxParent.Parent() + if ms.hasParentMsg() { + if sig.Kind() == SignalKindMultiplexer { + muxSig, err := sig.ToMultiplexer() + if err != nil { + panic(err) + } - case SignalParentKindMessage: - msgParent, err := parent.ToParentMessage() - if err != nil { - panic(err) - } + for tmpSigID, tmpSig := range muxSig.signals.entries() { + ms.parentMsg.signals.add(tmpSigID, tmpSig) + } - if sig.Kind() == SignalKindMultiplexer { - muxSig, err := sig.ToMultiplexer() - if err != nil { - panic(err) - } + for tmpName, tmpSigID := range muxSig.signalNames.entries() { + ms.parentMsg.signalNames.add(tmpName, tmpSigID) + } + } - for tmpSigID, tmpSig := range muxSig.signals.entries() { - msgParent.signals.add(tmpSigID, tmpSig) - } + ms.parentMsg.signals.add(id, sig) + ms.parentMsg.signalNames.add(name, id) - for tmpName, tmpSigID := range muxSig.signalNames.entries() { - msgParent.signalNames.add(tmpName, tmpSigID) - } + sig.setParentMsg(ms.parentMsg) + } - } + // if ms.hasParent() { + // parent := ms.Parent() + // for parent != nil { + // switch parent.GetSignalParentKind() { + // case SignalParentKindMultiplexerSignal: + // muxParent, err := parent.ToParentMultiplexerSignal() + // if err != nil { + // panic(err) + // } + // parent = muxParent.Parent() - msgParent.signals.add(id, sig) - msgParent.signalNames.add(name, id) + // case SignalParentKindMessage: + // msgParent, err := parent.ToParentMessage() + // if err != nil { + // panic(err) + // } - return - } - } - } + // if sig.Kind() == SignalKindMultiplexer { + // muxSig, err := sig.ToMultiplexer() + // if err != nil { + // panic(err) + // } + + // for tmpSigID, tmpSig := range muxSig.signals.entries() { + // msgParent.signals.add(tmpSigID, tmpSig) + // } + + // for tmpName, tmpSigID := range muxSig.signalNames.entries() { + // msgParent.signalNames.add(tmpName, tmpSigID) + // } + + // } + + // msgParent.signals.add(id, sig) + // msgParent.signalNames.add(name, id) + + // return + // } + // } + // } } func (ms *MultiplexerSignal) removeSignal(sig Signal) { @@ -135,91 +150,119 @@ func (ms *MultiplexerSignal) removeSignal(sig Signal) { ms.signals.remove(id) ms.signalNames.remove(name) - sig.setParent(nil) - - if ms.hasParent() { - parent := ms.Parent() - for parent != nil { - switch parent.GetSignalParentKind() { - case SignalParentKindMultiplexerSignal: - muxParent, err := parent.ToParentMultiplexerSignal() - if err != nil { - panic(err) - } - parent = muxParent.Parent() - - case SignalParentKindMessage: - msgParent, err := parent.ToParentMessage() - if err != nil { - panic(err) - } - - if sig.Kind() == SignalKindMultiplexer { - muxSig, err := sig.ToMultiplexer() - if err != nil { - panic(err) - } - - for tmpSigID := range muxSig.signals.entries() { - msgParent.signals.remove(tmpSigID) - } + // sig.setParent(nil) + sig.setParentMuxSig(nil) - for tmpName := range muxSig.signalNames.entries() { - msgParent.signalNames.remove(tmpName) - } - - } - - msgParent.signals.remove(id) - msgParent.signalNames.remove(name) - return + if ms.hasParentMsg() { + if sig.Kind() == SignalKindMultiplexer { + muxSig, err := sig.ToMultiplexer() + if err != nil { + panic(err) } - } - } -} - -func (ms *MultiplexerSignal) modifySignalName(sigID EntityID, newName string) error { - if ms.hasParent() { - parent := ms.Parent() - - loop: - for parent != nil { - switch parent.GetSignalParentKind() { - case SignalParentKindMultiplexerSignal: - muxParent, err := parent.ToParentMultiplexerSignal() - if err != nil { - panic(err) - } - parent = muxParent.Parent() - case SignalParentKindMessage: - msgParent, err := parent.ToParentMessage() - if err != nil { - panic(err) - } + for _, tmpSigID := range muxSig.signals.getKeys() { + ms.parentMsg.signals.remove(tmpSigID) + } - if err := msgParent.modifySignalName(sigID, newName); err != nil { - return err - } - break loop + for _, tmpName := range muxSig.signalNames.getKeys() { + ms.parentMsg.signalNames.remove(tmpName) } } - } - sig, err := ms.signals.getValue(sigID) - if err != nil { - return err - } + ms.parentMsg.signals.remove(id) + ms.parentMsg.signalNames.remove(name) + + sig.setParentMsg(nil) + } + + // if ms.hasParent() { + // parent := ms.Parent() + // for parent != nil { + // switch parent.GetSignalParentKind() { + // case SignalParentKindMultiplexerSignal: + // muxParent, err := parent.ToParentMultiplexerSignal() + // if err != nil { + // panic(err) + // } + // parent = muxParent.Parent() + + // case SignalParentKindMessage: + // msgParent, err := parent.ToParentMessage() + // if err != nil { + // panic(err) + // } + + // if sig.Kind() == SignalKindMultiplexer { + // muxSig, err := sig.ToMultiplexer() + // if err != nil { + // panic(err) + // } + + // for tmpSigID := range muxSig.signals.entries() { + // msgParent.signals.remove(tmpSigID) + // } + + // for tmpName := range muxSig.signalNames.entries() { + // msgParent.signalNames.remove(tmpName) + // } + + // } + + // msgParent.signals.remove(id) + // msgParent.signalNames.remove(name) + // return + // } + // } + // } +} - oldName := sig.Name() +// func (ms *MultiplexerSignal) modifySignalName(sigID EntityID, newName string) error { +// if ms.hasParent() { +// parent := ms.Parent() + +// loop: +// for parent != nil { +// switch parent.GetSignalParentKind() { +// case SignalParentKindMultiplexerSignal: +// muxParent, err := parent.ToParentMultiplexerSignal() +// if err != nil { +// panic(err) +// } +// parent = muxParent.Parent() + +// case SignalParentKindMessage: +// msgParent, err := parent.ToParentMessage() +// if err != nil { +// panic(err) +// } + +// if err := msgParent.modifySignalName(sigID, newName); err != nil { +// return err +// } +// break loop +// } +// } +// } + +// sig, err := ms.signals.getValue(sigID) +// if err != nil { +// return err +// } + +// oldName := sig.Name() + +// ms.signalNames.remove(oldName) +// ms.signalNames.add(newName, sigID) + +// return nil +// } - ms.signalNames.remove(oldName) - ms.signalNames.add(newName, sigID) +func (ms *MultiplexerSignal) verifySignalName(sigID EntityID, name string) error { - return nil -} + // if ms.hasParent() { + // return ms.parent.verifySignalName(sigID, name) + // } -func (ms *MultiplexerSignal) verifySignalName(sigID EntityID, name string) error { if ms.signalNames.hasKey(name) { tmpSigID, err := ms.signalNames.getValue(name) if err != nil { @@ -227,7 +270,7 @@ func (ms *MultiplexerSignal) verifySignalName(sigID EntityID, name string) error } if sigID != tmpSigID { - return &SignalNameError{ + return &NameError{ Name: name, Err: ErrIsDuplicated, } @@ -236,8 +279,13 @@ func (ms *MultiplexerSignal) verifySignalName(sigID EntityID, name string) error return nil } - if ms.hasParent() { - return ms.parent.verifySignalName(sigID, name) + if ms.hasParentMsg() { + if err := ms.parentMsg.verifySignalName(name); err != nil { + return &NameError{ + Name: name, + Err: err, + } + } } return nil diff --git a/signal.go b/signal.go index 10b0e97..0826c20 100644 --- a/signal.go +++ b/signal.go @@ -82,7 +82,10 @@ type Signal interface { // Parent returns the parent of the signal. Parent() SignalParent - setParent(parent SignalParent) + // setParent(parent SignalParent) + + setParentMsg(parentMsg *Message) + setParentMuxSig(parentMuxSig *MultiplexerSignal) // GetStartBit returns the start bit of the signal. GetStartBit() int @@ -103,7 +106,10 @@ type Signal interface { type signal struct { *attributeEntity - parent SignalParent + // parent SignalParent + + parentMsg *Message + parentMuxSig *MultiplexerSignal kind SignalKind relStartBit int @@ -113,30 +119,54 @@ func newSignal(name string, kind SignalKind) *signal { return &signal{ attributeEntity: newAttributeEntity(name, AttributeRefKindSignal), - parent: nil, + // parent: nil, + parentMsg: nil, + parentMuxSig: nil, kind: kind, relStartBit: 0, } } -func (s *signal) hasParent() bool { - return s.parent != nil +// func (s *signal) hasParent() bool { +// return s.parent != nil +// } + +func (s *signal) hasParentMsg() bool { + return s.parentMsg != nil +} + +func (s *signal) hasParentMuxSig() bool { + return s.parentMuxSig != nil } func (s *signal) modifySize(amount int) error { - if s.hasParent() { - return s.parent.modifySignalSize(s.EntityID(), amount) + // if s.hasParent() { + // return s.parent.modifySignalSize(s.EntityID(), amount) + // } + + if s.hasParentMuxSig() { + return s.parentMuxSig.modifySignalSize(s.EntityID(), amount) + } + + if s.hasParentMsg() { + return s.parentMsg.modifySignalSize(s.EntityID(), amount) } return nil } func (s *signal) errorf(err error) error { - sigErr := fmt.Errorf(`signal "%s": %w`, s.name, err) - if s.hasParent() { - return s.parent.errorf(sigErr) + sigErr := &SignalError{ + EntityID: s.entityID, + Name: s.name, + Err: err, } + + if s.hasParentMsg() { + return s.parentMsg.errorf(sigErr) + } + return sigErr } @@ -144,12 +174,21 @@ func (s *signal) Kind() SignalKind { return s.kind } +// TODO! remove this function func (s *signal) Parent() SignalParent { - return s.parent + return nil +} + +// func (s *signal) setParent(parent SignalParent) { +// s.parent = parent +// } + +func (s *signal) setParentMsg(parentMsg *Message) { + s.parentMsg = parentMsg } -func (s *signal) setParent(parent SignalParent) { - s.parent = parent +func (s *signal) setParentMuxSig(parentMuxSig *MultiplexerSignal) { + s.parentMuxSig = parentMuxSig } func (s *signal) getRelStartBit() int { @@ -170,17 +209,21 @@ func (s *signal) stringify(b *strings.Builder, tabs int) { } func (s *signal) GetStartBit() int { - if s.hasParent() { - if s.parent.GetSignalParentKind() == SignalParentKindMultiplexerSignal { - muxParent, err := s.parent.ToParentMultiplexerSignal() - if err != nil { - panic(err) - } - - return muxParent.GetStartBit() + muxParent.GetGroupCountSize() + s.relStartBit - } + if s.hasParentMuxSig() { + return s.parentMuxSig.GetStartBit() + s.parentMuxSig.GetGroupCountSize() + s.relStartBit } + // if s.hasParent() { + // if s.parent.GetSignalParentKind() == SignalParentKindMultiplexerSignal { + // muxParent, err := s.parent.ToParentMultiplexerSignal() + // if err != nil { + // panic(err) + // } + + // return muxParent.GetStartBit() + muxParent.GetGroupCountSize() + s.relStartBit + // } + // } + return s.relStartBit } @@ -189,16 +232,47 @@ func (s *signal) UpdateName(newName string) error { return nil } - if s.hasParent() { - if err := s.parent.verifySignalName(s.entityID, newName); err != nil { - return s.errorf(fmt.Errorf(`cannot update name to "%s" : %w`, newName, err)) + if s.hasParentMuxSig() { + if err := s.parentMuxSig.verifySignalName(s.entityID, newName); err != nil { + return &UpdateNameError{Err: err} } - if err := s.parent.modifySignalName(s.entityID, newName); err != nil { - return s.errorf(fmt.Errorf(`cannot update name to "%s" : %w`, newName, err)) + if s.hasParentMsg() { + if err := s.parentMsg.verifySignalName(newName); err != nil { + return &UpdateNameError{Err: err} + } + + s.parentMsg.signalNames.remove(s.name) + s.parentMsg.signalNames.add(newName, s.entityID) } + + s.parentMuxSig.signalNames.remove(s.name) + s.parentMuxSig.signalNames.add(newName, s.entityID) + + s.name = newName + + return nil + } + + if s.hasParentMsg() { + if err := s.parentMsg.verifySignalName(newName); err != nil { + return &UpdateNameError{Err: err} + } + + s.parentMsg.signalNames.remove(s.name) + s.parentMsg.signalNames.add(newName, s.entityID) } + // if s.hasParent() { + // if err := s.parent.verifySignalName(s.entityID, newName); err != nil { + // return s.errorf(fmt.Errorf(`cannot update name to "%s" : %w`, newName, err)) + // } + + // if err := s.parent.modifySignalName(s.entityID, newName); err != nil { + // return s.errorf(fmt.Errorf(`cannot update name to "%s" : %w`, newName, err)) + // } + // } + s.name = newName return nil diff --git a/signal_enum.go b/signal_enum.go index 9b384dd..ac9af19 100644 --- a/signal_enum.go +++ b/signal_enum.go @@ -79,13 +79,27 @@ func (se *SignalEnum) verifyValueIndex(index int) error { newSize := calcSizeFromValue(index) for _, tmpSig := range se.parentSignals.entries() { - if !tmpSig.hasParent() { - continue + // if !tmpSig.hasParent() { + // continue + // } + + // if err := tmpSig.parent.verifySignalSizeAmount(tmpSig.entityID, newSize-prevSize); err != nil { + // se.parErrID = tmpSig.entityID + // return err + // } + + if tmpSig.hasParentMsg() { + if err := tmpSig.parentMsg.verifySignalSizeAmount(tmpSig.entityID, newSize-prevSize); err != nil { + se.parErrID = tmpSig.entityID + return err + } } - if err := tmpSig.parent.verifySignalSizeAmount(tmpSig.entityID, newSize-prevSize); err != nil { - se.parErrID = tmpSig.entityID - return err + if tmpSig.hasParentMuxSig() { + if err := tmpSig.parentMuxSig.verifySignalSizeAmount(tmpSig.entityID, newSize-prevSize); err != nil { + se.parErrID = tmpSig.entityID + return err + } } } }