Skip to content

Commit

Permalink
g
Browse files Browse the repository at this point in the history
  • Loading branch information
komuw committed Dec 31, 2024
1 parent 348e6d2 commit f1f50a4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
17 changes: 12 additions & 5 deletions internal/mx/mx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package mx

import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -145,13 +146,19 @@ func (m Muxer) AddRoute(rt Route) error {
return nil
}

// Merge combines mxs into m. The resulting muxer uses the opts & notFoundHandler of m.
func (m Muxer) Merge(mxs ...Muxer) (Muxer, error) {
if len(mxs) < 1 {
return m, nil
// Merge combines mxs into one. The resulting muxer uses the opts & notFoundHandler of muxer at index 0.
func Merge(mxs ...Muxer) (Muxer, error) {
_len := len(mxs)

if _len <= 0 {
return Muxer{}, errors.New("ong/mux: no muxer")
}
if _len == 1 {
return mxs[0], nil
}

for _, v := range mxs {
m := mxs[0]
for _, v := range mxs[1:] {
m.router.routes = append(m.router.routes, v.router.routes...)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/mx/mx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func TestMux(t *testing.T) {
mux2, err := New(config.WithOpts(domain, httpsPort, tst.SecretKey(), config.DirectIpStrategy, l), nil, rt2, rt3)
attest.Ok(t, err)

m, err := mux1.Merge(mux2)
m, err := Merge(mux1, mux2)
attest.Ok(t, err)

attest.Equal(t, m.opt, mux1.opt)
Expand All @@ -452,7 +452,7 @@ func TestMux(t *testing.T) {
mux2, err := New(config.WithOpts(domain, httpsPort, tst.SecretKey(), config.DirectIpStrategy, l), nil, rt2, rt3)
attest.Ok(t, err)

_, errM := mux1.Merge(mux2)
_, errM := Merge(mux1, mux2)
attest.Error(t, errM)
rStr := errM.Error()
attest.Subsequence(t, rStr, "would conflict")
Expand Down
6 changes: 3 additions & 3 deletions mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ func (m Muxer) Unwrap() mx.Muxer {
return m.internalMux
}

// Merge combines mxs into m. The resulting muxer uses the opts & notFoundHandler of m.
func (m Muxer) Merge(mxs ...Muxer) (Muxer, error) {
// Merge combines mxs into one. The resulting muxer uses the opts & notFoundHandler of muxer at index 0.
func Merge(mxs ...Muxer) (Muxer, error) {
mi := []mx.Muxer{}
for _, v := range mxs {
mi = append(mi, v.internalMux)
}

mm, err := m.internalMux.Merge(mi...)
mm, err := mx.Merge(mi...)
if err != nil {
return Muxer{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions mux/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestMerge(t *testing.T) {
mx1 := New(config.DevOpts(l, "secretKey12@34String"), nil, rt1...)
mx2 := New(config.DevOpts(l, "secretKey12@34String"), nil, rt2...)

_, err := mx1.Merge(mx2)
_, err := Merge(mx1, mx2)
attest.Ok(t, err)
})

Expand All @@ -92,7 +92,7 @@ func TestMerge(t *testing.T) {
mx1 := New(config.DevOpts(l, "secretKey12@34String"), nil, rt1...)
mx2 := New(config.DevOpts(l, "secretKey12@34String"), nil, rt2...)

_, err := mx1.Merge(mx2)
_, err := Merge(mx1, mx2)
attest.Error(t, err)
attest.Subsequence(t, err.Error(), "would conflict")
})
Expand Down

0 comments on commit f1f50a4

Please sign in to comment.