From f1f50a4b379233c6fc3318c60180405c7531a865 Mon Sep 17 00:00:00 2001 From: komuw Date: Tue, 31 Dec 2024 16:04:56 +0300 Subject: [PATCH] g --- internal/mx/mx.go | 17 ++++++++++++----- internal/mx/mx_test.go | 4 ++-- mux/mux.go | 6 +++--- mux/mux_test.go | 4 ++-- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/internal/mx/mx.go b/internal/mx/mx.go index 8222c012..f74f451a 100644 --- a/internal/mx/mx.go +++ b/internal/mx/mx.go @@ -5,6 +5,7 @@ package mx import ( "context" + "errors" "fmt" "net/http" "net/url" @@ -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...) } diff --git a/internal/mx/mx_test.go b/internal/mx/mx_test.go index cd648e1b..8bc909d3 100644 --- a/internal/mx/mx_test.go +++ b/internal/mx/mx_test.go @@ -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) @@ -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") diff --git a/mux/mux.go b/mux/mux.go index 99b986bc..5bfb0606 100644 --- a/mux/mux.go +++ b/mux/mux.go @@ -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 } diff --git a/mux/mux_test.go b/mux/mux_test.go index 99171653..2c6aef53 100644 --- a/mux/mux_test.go +++ b/mux/mux_test.go @@ -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) }) @@ -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") })