Skip to content

Commit 8ea9634

Browse files
authored
Merge pull request #3 from edadeal/feature/must_publish
MustPublish method, State Stringer
2 parents 8ade2b9 + 8e66050 commit 8ea9634

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

lepus.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package lepus
22

33
import (
44
"errors"
5+
"fmt"
56
"strconv"
67
"sync"
78
"sync/atomic"
@@ -10,18 +11,6 @@ import (
1011
"github.com/streadway/amqp"
1112
)
1213

13-
// State indicates publishing state of message
14-
type State int32
15-
16-
// states
17-
const (
18-
StateUnknown State = iota
19-
StatePublished
20-
StateReturned
21-
StateTimeout
22-
StateClosed
23-
)
24-
2514
type info struct {
2615
state int32
2716
err error
@@ -234,3 +223,16 @@ func (d *Delivery) NackDelayed(multiple, mandatory, immediate bool) (State, erro
234223
Body: d.Delivery.Body,
235224
})
236225
}
226+
227+
// MustPublish can be used as a wrapper around `PublishAndWait` and
228+
// `NackDelayed` methods if you didn't want to process error and state
229+
// separately.
230+
func MustPublish(s State, err error) error {
231+
if err != nil {
232+
return err
233+
}
234+
if s != StatePublished {
235+
return fmt.Errorf("Message publishing failed. Result state: %s", s)
236+
}
237+
return nil
238+
}

state.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package lepus
2+
3+
import "strconv"
4+
5+
// State indicates publishing state of message
6+
type State int32
7+
8+
// states
9+
const (
10+
StateUnknown State = iota // Unknown
11+
StatePublished // Published
12+
StateReturned // Returned
13+
StateTimeout // Timeout
14+
StateClosed // Closed
15+
)
16+
17+
const _State_name = "UnknownPublishedReturnedTimeoutClosed"
18+
19+
var _State_index = [...]uint8{0, 7, 16, 24, 31, 37}
20+
21+
func (i State) String() string {
22+
if i < 0 || i >= State(len(_State_index)-1) {
23+
return "State(" + strconv.FormatInt(int64(i), 10) + ")"
24+
}
25+
return _State_name[_State_index[i]:_State_index[i+1]]
26+
}

state_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package lepus
2+
3+
import "testing"
4+
5+
var stateStringerCases = []struct {
6+
name string
7+
state State
8+
expected string
9+
}{
10+
{"Unknown", StateUnknown, "Unknown"},
11+
{"Published", StatePublished, "Published"},
12+
{"Returned", StateReturned, "Returned"},
13+
{"Timeout", StateTimeout, "Timeout"},
14+
{"Closed", StateClosed, "Closed"},
15+
}
16+
17+
func TestStateStringer(t *testing.T) {
18+
for _, c := range stateStringerCases {
19+
t.Run(c.name, func(t *testing.T) {
20+
if c.state.String() != c.expected {
21+
t.Fatalf("Expecting '%s', got '%s'", c.expected, c.state.String())
22+
}
23+
})
24+
}
25+
}

0 commit comments

Comments
 (0)