Skip to content

Commit

Permalink
sync mac
Browse files Browse the repository at this point in the history
  • Loading branch information
FerroO2000 committed Apr 22, 2024
1 parent 5350fce commit 65654e2
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 186 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 20 additions & 10 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }
Expand All @@ -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 }
Expand All @@ -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 }
Expand All @@ -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 }
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
104 changes: 81 additions & 23 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -274,22 +274,73 @@ 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))
}

if err := m.signalPayload.append(signal); err != nil {
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
}
Expand All @@ -299,18 +350,21 @@ 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))
}

if err := m.signalPayload.verifyAndInsert(signal, startBit); err != nil {
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
}
Expand All @@ -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)

Expand All @@ -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()
Expand Down
Loading

0 comments on commit 65654e2

Please sign in to comment.