From e5bd03eafaec5dff57ed202189e24d9d91247a73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20D=C3=B6ll?= Date: Wed, 14 Aug 2024 14:34:32 +0000 Subject: [PATCH] refactor: remove old pkg package --- attrib.go | 8 +- components/collapsible/collapsible.go | 9 ++- components/dropdowns/dropdown.go | 5 +- components/progress/progress.go | 29 ++++--- comps_test.go | 3 +- examples/main.go | 2 +- htmx.go | 7 +- sse/sse.go | 4 +- swap.go | 9 ++- utils.go | 109 -------------------------- utils_test.go | 41 ---------- 11 files changed, 45 insertions(+), 181 deletions(-) diff --git a/attrib.go b/attrib.go index b52d8c1..5201084 100644 --- a/attrib.go +++ b/attrib.go @@ -5,6 +5,8 @@ package htmx import ( "fmt" + + "github.com/zeiss/pkg/conv" ) // HxEventType represents the type of htmx event. @@ -25,7 +27,7 @@ const ( // HxBoost sets the hx-boost attribute to enable or disable boosting. func HxBoost(v bool) Node { - return Attribute("hx-boost", AsStr(v)) + return Attribute("hx-boost", conv.String(v)) } // HxGet sets the hx-get attribute to specify the URL for GET requests. @@ -40,7 +42,7 @@ func HxPost(url string) Node { // HxPushUrl sets the hx-push-url attribute to enable or disable URL pushing. func HxPushUrl(v bool) Node { - return Attribute("hx-boost", AsStr(v)) + return Attribute("hx-boost", conv.String(v)) } // HxTarget sets the hx-target attribute to specify the target element for the response. @@ -170,7 +172,7 @@ func HxDisabledElt(target string) Node { // HxValidate sets the hx-validate attribute to enable or disable form validation. func HxValidate(v bool) Node { - return Attribute("hx-validate", AsStr(v)) + return Attribute("hx-validate", conv.String(v)) } // HxInclude sets the hx-include attribute to specify the target element for inclusion. diff --git a/components/collapsible/collapsible.go b/components/collapsible/collapsible.go index 5dc8671..f61d896 100644 --- a/components/collapsible/collapsible.go +++ b/components/collapsible/collapsible.go @@ -1,6 +1,9 @@ package collapsible -import htmx "github.com/zeiss/fiber-htmx" +import ( + htmx "github.com/zeiss/fiber-htmx" + "github.com/zeiss/pkg/conv" +) // CollapseProps is a component that can be expanded and collapsed. type CollapseProps struct { @@ -18,7 +21,7 @@ func Collapse(props CollapseProps, children ...htmx.Node) htmx.Node { }, props.ClassNames, ), - htmx.Attribute("tabindex", htmx.IntAsString(props.TabIndex)), + htmx.Attribute("tabindex", conv.String(props.TabIndex)), htmx.Group(children...), ) } @@ -26,7 +29,7 @@ func Collapse(props CollapseProps, children ...htmx.Node) htmx.Node { // CollapseArrow is a component that can be expanded and collapsed. func CollapseArrow(p CollapseProps, children ...htmx.Node) htmx.Node { return htmx.Div( - htmx.Attribute("tabindex", htmx.IntAsString(p.TabIndex)), + htmx.Attribute("tabindex", conv.String(p.TabIndex)), htmx.Merge( htmx.ClassNames{ "collapse": true, diff --git a/components/dropdowns/dropdown.go b/components/dropdowns/dropdown.go index ab629a8..e9bbe84 100644 --- a/components/dropdowns/dropdown.go +++ b/components/dropdowns/dropdown.go @@ -2,6 +2,7 @@ package dropdowns import ( htmx "github.com/zeiss/fiber-htmx" + "github.com/zeiss/pkg/conv" ) // DropdownProps represents the properties for a dropdown element. @@ -31,7 +32,7 @@ type DropdownButtonProps struct { // DropdownButton generates a dropdown summary element based on the provided properties. func DropdownButton(p DropdownButtonProps, children ...htmx.Node) htmx.Node { return htmx.Div( - htmx.TabIndex(htmx.IntAsString(p.TabIndex)), + htmx.TabIndex(conv.String(p.TabIndex)), htmx.Role("button"), htmx.Merge( htmx.ClassNames{ @@ -55,7 +56,7 @@ type DropdownMenuItemsProps struct { func DropdownMenuItems(p DropdownMenuItemsProps, children ...htmx.Node) htmx.Node { return htmx.Ul( htmx.TabIndex( - htmx.IntAsString(p.TabIndex), + conv.String(p.TabIndex), ), htmx.Merge( htmx.ClassNames{ diff --git a/components/progress/progress.go b/components/progress/progress.go index 1685709..dbe3e92 100644 --- a/components/progress/progress.go +++ b/components/progress/progress.go @@ -1,6 +1,9 @@ package progress -import htmx "github.com/zeiss/fiber-htmx" +import ( + htmx "github.com/zeiss/fiber-htmx" + "github.com/zeiss/pkg/conv" +) // ProgressProps is a struct that contains the props of the progress component type ProgressProps struct { @@ -19,8 +22,8 @@ func Progress(p ProgressProps, children ...htmx.Node) htmx.Node { }, p.ClassNames, ), - htmx.Attribute("max", htmx.IntAsString(p.Max)), - htmx.Attribute("value", htmx.IntAsString(p.Value)), + htmx.Attribute("max", conv.String(p.Max)), + htmx.Attribute("value", conv.String(p.Value)), htmx.Group(children...), ) } @@ -36,8 +39,8 @@ func ProgressPrimary(p ProgressProps, children ...htmx.Node) htmx.Node { }, p.ClassNames, ), - htmx.Attribute("max", htmx.IntAsString(p.Max)), - htmx.Attribute("value", htmx.IntAsString(p.Value)), + htmx.Attribute("max", conv.String(p.Max)), + htmx.Attribute("value", conv.String(p.Value)), htmx.Group(children...), ) } @@ -53,8 +56,8 @@ func ProgressSecondary(p ProgressProps, children ...htmx.Node) htmx.Node { }, p.ClassNames, ), - htmx.Attribute("max", htmx.IntAsString(p.Max)), - htmx.Attribute("value", htmx.IntAsString(p.Value)), + htmx.Attribute("max", conv.String(p.Max)), + htmx.Attribute("value", conv.String(p.Value)), htmx.Group(children...), ) } @@ -70,8 +73,8 @@ func ProgressSuccess(p ProgressProps, children ...htmx.Node) htmx.Node { }, p.ClassNames, ), - htmx.Attribute("max", htmx.IntAsString(p.Max)), - htmx.Attribute("value", htmx.IntAsString(p.Value)), + htmx.Attribute("max", conv.String(p.Max)), + htmx.Attribute("value", conv.String(p.Value)), htmx.Group(children...), ) } @@ -87,8 +90,8 @@ func ProgressInfo(p ProgressProps, children ...htmx.Node) htmx.Node { }, p.ClassNames, ), - htmx.Attribute("max", htmx.IntAsString(p.Max)), - htmx.Attribute("value", htmx.IntAsString(p.Value)), + htmx.Attribute("max", conv.String(p.Max)), + htmx.Attribute("value", conv.String(p.Value)), htmx.Group(children...), ) } @@ -103,8 +106,8 @@ func ProgressWarning(p ProgressProps, children ...htmx.Node) htmx.Node { "progress-warning": true, }, ), - htmx.Attribute("max", htmx.IntAsString(p.Max)), - htmx.Attribute("value", htmx.IntAsString(p.Value)), + htmx.Attribute("max", conv.String(p.Max)), + htmx.Attribute("value", conv.String(p.Value)), htmx.Group(children...), ) } diff --git a/comps_test.go b/comps_test.go index f3ec026..b43143b 100644 --- a/comps_test.go +++ b/comps_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" htmx "github.com/zeiss/fiber-htmx" + "github.com/zeiss/pkg/conv" ) func BenchmarkElement(b *testing.B) { @@ -32,7 +33,7 @@ func ExampleElement_tag() { } func ExampleAttribute_bool() { - _ = htmx.Attribute("disabled", htmx.AsStr(true)).Render(os.Stdout) + _ = htmx.Attribute("disabled", conv.String(true)).Render(os.Stdout) // Output: disabled="true" } diff --git a/examples/main.go b/examples/main.go index 09a2368..2d597c0 100644 --- a/examples/main.go +++ b/examples/main.go @@ -9,7 +9,6 @@ import ( "strconv" "time" - "github.com/katallaxie/pkg/server" htmx "github.com/zeiss/fiber-htmx" "github.com/zeiss/fiber-htmx/bundle" "github.com/zeiss/fiber-htmx/components/alerts" @@ -28,6 +27,7 @@ import ( "github.com/zeiss/fiber-htmx/components/toasts" "github.com/zeiss/fiber-htmx/components/utils" "github.com/zeiss/fiber-htmx/sse" + "github.com/zeiss/pkg/server" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/logger" diff --git a/htmx.go b/htmx.go index de10535..8dac896 100644 --- a/htmx.go +++ b/htmx.go @@ -8,6 +8,7 @@ import ( "github.com/gofiber/fiber/v2" authz "github.com/zeiss/fiber-authz" + "github.com/zeiss/pkg/conv" ) // The contextKey type is unexported to prevent collisions with context keys defined in @@ -112,7 +113,7 @@ func HxTriggers(c *fiber.Ctx, target string) { // Boosted returns true if the request is boosted. func Boosted(c *fiber.Ctx) bool { - return AsBool(c.Get(HxRequestHeaderBoosted.String())) + return conv.Bool(c.Get(HxRequestHeaderBoosted.String())) } // CurrentURL returns the current URL. @@ -122,7 +123,7 @@ func CurrentURL(c *fiber.Ctx) string { // HistoryRestoreRequest returns true if the request is a history restore request. func HistoryRestoreRequest(c *fiber.Ctx) bool { - return AsBool(c.Get(HxRequestHeaderHistoryRestoreRequest.String())) + return conv.Bool(c.Get(HxRequestHeaderHistoryRestoreRequest.String())) } // Prompt returns the prompt. @@ -132,7 +133,7 @@ func Prompt(c *fiber.Ctx) string { // Request returns true if the request is an htmx request. func Request(c *fiber.Ctx) bool { - return AsBool(c.Get(HxRequestHeaderRequest.String())) + return conv.Bool(c.Get(HxRequestHeaderRequest.String())) } // Targets is a helper function to get the target. diff --git a/sse/sse.go b/sse/sse.go index bee2128..d751790 100644 --- a/sse/sse.go +++ b/sse/sse.go @@ -10,8 +10,8 @@ import ( "github.com/gofiber/fiber/v2" "github.com/google/uuid" - "github.com/katallaxie/pkg/server" "github.com/valyala/fasthttp" + "github.com/zeiss/pkg/server" ) var _ Event = (*EventImpl)(nil) @@ -98,7 +98,7 @@ func (b *BroadcastManagerImpl) Remove(client Client) { func (b *BroadcastManagerImpl) Start(ctx context.Context, ready server.ReadyFunc, run server.RunFunc) func() error { return func() error { if b.poolSize < 1 { - return server.NewError(fmt.Errorf("pool size must be greater than 0")) + return server.NewServerError(fmt.Errorf("pool size must be greater than 0")) } b.startWorkers(ctx) diff --git a/swap.go b/swap.go index 012c4b6..949e619 100644 --- a/swap.go +++ b/swap.go @@ -4,6 +4,9 @@ import ( "fmt" "strings" "time" + + "github.com/zeiss/pkg/cast" + "github.com/zeiss/pkg/conv" ) var ( @@ -181,15 +184,15 @@ func (s *Swap) String() string { } if s.transition != nil { - parts = append(parts, fmt.Sprintf("transition:%s", AsStr(*s.transition))) + parts = append(parts, fmt.Sprintf("transition:%s", conv.String(cast.Value(s.transition)))) } if s.ignoreTitle != nil { - parts = append(parts, fmt.Sprintf("ignoreTitle:%s", AsStr(*s.ignoreTitle))) + parts = append(parts, fmt.Sprintf("ignoreTitle:%s", conv.String(cast.Value(s.ignoreTitle)))) } if s.focusScroll != nil { - parts = append(parts, fmt.Sprintf("focus-scroll:%s", AsStr(*s.focusScroll))) + parts = append(parts, fmt.Sprintf("focus-scroll:%s", conv.String(cast.Value(s.focusScroll)))) } if s.timing != nil { diff --git a/utils.go b/utils.go index cb8ec35..5d77fba 100644 --- a/utils.go +++ b/utils.go @@ -1,119 +1,10 @@ package htmx import ( - "context" "regexp" - "strconv" "strings" - "sync" - - "github.com/gofiber/fiber/v2" - "github.com/katallaxie/pkg/utils" ) -// ResolveFunc is a function that returns a context. -type ResolveFunc func(ctx *fiber.Ctx) (any, any, error) - -// Bind is a function that returns a BindFactory. -func Resolve(ctx *fiber.Ctx, funcs ...ResolveFunc) error { - c := struct { - wg sync.WaitGroup - errOnce sync.Once - err error - }{} - - for _, f := range funcs { - f := f - - c.wg.Add(1) - go func() { - defer c.wg.Done() - - k, v, err := f(ctx) - if err != nil { - c.errOnce.Do(func() { - c.err = err - }) - return - } - ctx.SetUserContext(context.WithValue(ctx.UserContext(), k, v)) - }() - } - - c.wg.Wait() - - return c.err -} - -// Locals is a method that returns the local values. -func Locals[V any](c *fiber.Ctx, key any, value ...V) V { - var v V - var ok bool - - if len(value) > 0 { - v, ok = c.Locals(key, value).(V) - } else { - v, ok = c.Locals(key).(V) - } - - if !ok { - return utils.Zero[V]() - } - - return v -} - -// Values is a helper type for user context. -func Values[V any](ctx context.Context, key any) V { - var v V - var ok bool - - v, ok = ctx.Value(key).(V) - if !ok { - return utils.Zero[V]() - } - - return v -} - -// ValuePtr is a helper function that returns a pointer to the provided value. -func ValuePtr[V any](v V) *V { - return &v -} - -// PtrValue is a helper function that returns the value of the provided pointer. -func PtrValue[V any](v *V) V { - if v == nil { - return utils.Zero[V]() - } - - return *v -} - -// AsValue is a helper function that returns a value based on the provided pointer. -func AsValue[T any](v any) T { - if v == nil { - return utils.Zero[T]() - } - - return v.(T) -} - -// AsBool is a helper function that returns a boolean value based on the provided string. -func AsBool(str string) bool { - return strings.EqualFold(str, "true") -} - -// AsStr is a helper function that returns a string value based on the provided boolean. -func AsStr(v bool) string { - return strconv.FormatBool(v) -} - -// IntAsString is a helper function that returns a string value based on the provided integer. -func IntAsString(v int) string { - return strconv.Itoa(v) -} - // Merge returns a new ClassNames object that is the result of merging the provided ClassNames objects. func Merge(classNames ...ClassNames) ClassNames { merged := ClassNames{} diff --git a/utils_test.go b/utils_test.go index fbc8379..531b74f 100644 --- a/utils_test.go +++ b/utils_test.go @@ -1,7 +1,6 @@ package htmx_test import ( - "context" "testing" htmx "github.com/zeiss/fiber-htmx" @@ -9,23 +8,6 @@ import ( "github.com/stretchr/testify/assert" ) -func TestAsBool(t *testing.T) { - assert.True(t, htmx.AsBool("true")) - assert.False(t, htmx.AsBool("false")) - assert.True(t, htmx.AsBool("True")) - assert.False(t, htmx.AsBool("False")) -} - -func TestAsStr(t *testing.T) { - assert.Equal(t, "true", htmx.AsStr(true)) - assert.Equal(t, "false", htmx.AsStr(false)) -} - -func TestIntAsString(t *testing.T) { - assert.Equal(t, "1", htmx.IntAsString(1)) - assert.Equal(t, "0", htmx.IntAsString(0)) -} - func TestMerge(t *testing.T) { tests := []struct { name string @@ -48,26 +30,3 @@ func TestMerge(t *testing.T) { }) } } - -func Test_Values(t *testing.T) { - type contextKey int - const testKey contextKey = iota - - tests := []struct { - name string - in context.Context - out interface{} - }{ - { - name: "values", - in: context.WithValue(context.Background(), testKey, "value"), - out: "value", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - assert.Equal(t, tt.out, htmx.Values[string](tt.in, testKey)) - }) - } -}