Skip to content

Commit

Permalink
perf: ⚡ share a validator instance across packages
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Oct 30, 2024
1 parent 6fa0bf6 commit 81bf9e8
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 116 deletions.
19 changes: 5 additions & 14 deletions internal/agent/ui/fyneUI/fyneUI.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ import (
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/go-playground/validator/v10"

agentvalidator "github.com/joshuar/go-hass-agent/internal/validation"

"github.com/joshuar/go-hass-agent/internal/agent/ui"
"github.com/joshuar/go-hass-agent/internal/hass"
"github.com/joshuar/go-hass-agent/internal/logging"
"github.com/joshuar/go-hass-agent/internal/preferences"
)

var validate *validator.Validate

var (
//nolint:stylecheck
//lint:ignore ST1005 these are not standard error messages
Expand All @@ -59,10 +58,6 @@ type FyneUI struct {
logger *slog.Logger
}

func init() {
validate = validator.New(validator.WithRequiredStructEnabled())
}

// New FyneUI sets up the UI for the agent.
func NewFyneUI(ctx context.Context) *FyneUI {
appUI := &FyneUI{
Expand Down Expand Up @@ -546,10 +541,8 @@ func longestString(stringList []string) string {
// httpValidator is a custom fyne validator that will validate a string is a
// valid http/https URL.
func httpValidator() fyne.StringValidator {
v := validator.New()

return func(text string) error {
if v.Var(text, "http_url") != nil {
if agentvalidator.Validate.Var(text, "http_url") != nil {
return ErrInvalidURL
}

Expand All @@ -565,7 +558,7 @@ func httpValidator() fyne.StringValidator {
// valid http/https URL.
func uriValidator() fyne.StringValidator {
return func(text string) error {
if validate.Var(text, "uri") != nil {
if agentvalidator.Validate.Var(text, "uri") != nil {
return ErrInvalidURI
}

Expand All @@ -590,10 +583,8 @@ func hostPortValidator(msg string) fyne.StringValidator {
errMsg = ErrInvalidHostPort
}

v := validator.New()

return func(text string) error {
if v.Var(text, "hostname_port") != nil {
if agentvalidator.Validate.Var(text, "hostname_port") != nil {
return errMsg
}
// if _, err := url.Parse(text); err != nil {
Expand Down
10 changes: 7 additions & 3 deletions internal/hass/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
//revive:disable:unused-receiver
package event

import "fmt"
import (
"fmt"

"github.com/joshuar/go-hass-agent/internal/validation"
)

const (
requestTypeEvent = "fire_event"
Expand All @@ -16,9 +20,9 @@ type Event struct {
}

func (e *Event) Validate() error {
err := validate.Struct(e)
err := validation.Validate.Struct(e)
if err != nil {
return fmt.Errorf("event is invalid: %s", parseValidationErrors(err))
return fmt.Errorf("event is invalid: %s", validation.ParseValidationErrors(err))
}

return nil
Expand Down
13 changes: 7 additions & 6 deletions internal/hass/sensor/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"

"github.com/joshuar/go-hass-agent/internal/hass/sensor/types"
"github.com/joshuar/go-hass-agent/internal/validation"
)

const (
Expand All @@ -33,9 +34,9 @@ type State struct {
}

func (s *State) Validate() error {
err := validate.Struct(s)
err := validation.Validate.Struct(s)
if err != nil {
return fmt.Errorf("sensor state is invalid: %s", parseValidationErrors(err))
return fmt.Errorf("sensor state is invalid: %s", validation.ParseValidationErrors(err))
}

return nil
Expand Down Expand Up @@ -75,9 +76,9 @@ type Entity struct {
}

func (e *Entity) Validate() error {
err := validate.Struct(e)
err := validation.Validate.Struct(e)
if err != nil {
return fmt.Errorf("sensor is invalid: %s", parseValidationErrors(err))
return fmt.Errorf("sensor is invalid: %s", validation.ParseValidationErrors(err))
}

return nil
Expand Down Expand Up @@ -132,9 +133,9 @@ type Location struct {
}

func (l *Location) Validate() error {
err := validate.Struct(l)
err := validation.Validate.Struct(l)
if err != nil {
return fmt.Errorf("location is invalid: %s", parseValidationErrors(err))
return fmt.Errorf("location is invalid: %s", validation.ParseValidationErrors(err))
}

return nil
Expand Down
40 changes: 0 additions & 40 deletions internal/hass/sensor/validation.go

This file was deleted.

6 changes: 4 additions & 2 deletions internal/preferences/prefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

"github.com/adrg/xdg"
"github.com/pelletier/go-toml/v2"

"github.com/joshuar/go-hass-agent/internal/validation"
)

const (
Expand Down Expand Up @@ -122,9 +124,9 @@ func DefaultPreferences(file string) *Preferences {
}

func (p *Preferences) Validate() error {
err := validate.Struct(p)
err := validation.Validate.Struct(p)
if err != nil {
return fmt.Errorf("%w: %s", ErrValidationFailed, parseValidationErrors(err))
return fmt.Errorf("validation failed: %s", validation.ParseValidationErrors(err))
}

return nil
Expand Down
6 changes: 4 additions & 2 deletions internal/preferences/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package preferences

import (
"fmt"

"github.com/joshuar/go-hass-agent/internal/validation"
)

type Registration struct {
Expand All @@ -23,9 +25,9 @@ func DefaultRegistrationPreferences() *Registration {
}

func (p *Registration) Validate() error {
err := validate.Struct(p)
err := validation.Validate.Struct(p)
if err != nil {
return fmt.Errorf("%w: %s", ErrValidationFailed, parseValidationErrors(err))
return fmt.Errorf("validation failed: %s", validation.ParseValidationErrors(err))
}

return nil
Expand Down
45 changes: 0 additions & 45 deletions internal/preferences/validation.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// Copyright 2024 Joshua Rich <[email protected]>.
// SPDX-License-Identifier: MIT

package event
package validation

import (
"strings"

"github.com/go-playground/validator/v10"
)

var validate *validator.Validate
var Validate *validator.Validate

func init() {
validate = validator.New(validator.WithRequiredStructEnabled())
Validate = validator.New(validator.WithRequiredStructEnabled())
}

//nolint:errorlint
//revive:disable:unhandled-error
func parseValidationErrors(validation error) string {
func ParseValidationErrors(validation error) string {
validationErrs, ok := validation.(validator.ValidationErrors)
if !ok {
return "internal validation error"
Expand Down

0 comments on commit 81bf9e8

Please sign in to comment.