Skip to content

Commit

Permalink
refactor structure
Browse files Browse the repository at this point in the history
  • Loading branch information
nieomylnieja committed Aug 8, 2024
1 parent 02c0027 commit 320f5a5
Show file tree
Hide file tree
Showing 47 changed files with 933 additions and 857 deletions.
7 changes: 0 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@ module github.com/nobl9/govy
go 1.22

require (
github.com/nobl9/go-yaml v1.0.1
github.com/nobl9/nobl9-go v0.83.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
29 changes: 0 additions & 29 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,42 +1,13 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/nobl9/go-yaml v1.0.1 h1:Aj1kSaYdRQTKlvS6ihvXzQJhCpoHhtf9nfA95zqWH4Q=
github.com/nobl9/go-yaml v1.0.1/go.mod h1:t7vCO8ctYdBweZxU5lUgxzAw31+ZcqJYeqRtrv+5RHI=
github.com/nobl9/nobl9-go v0.83.0 h1:2In5WdZh/GiChSIRLoxhMJf5DKdKCS492bURQJC7my8=
github.com/nobl9/nobl9-go v0.83.0/go.mod h1:Fx4m7n32rq+f0N+ezgx8+JbhGg89T6TikiGC/QlRE14=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
107 changes: 107 additions & 0 deletions internal/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package internal

import (
"encoding/json"
"fmt"
"reflect"
"strings"
)

// RuleSetError is a container for transferring multiple errors reported by [govy.RuleSet].
// It is intentionally not exported as it is only an intermediate stage before the
// aggregated errors are flattened.
type RuleSetError []error

func (r RuleSetError) Error() string {
b := new(strings.Builder)
JoinErrors(b, r, "")
return b.String()
}

// JoinErrors joins multiple errors into a single pretty-formmated string.
func JoinErrors[T error](b *strings.Builder, errs []T, indent string) {
for i, err := range errs {
buildErrorMessage(b, err.Error(), indent)
if i < len(errs)-1 {
b.WriteString("\n")
}
}
}

const listPoint = "- "

func buildErrorMessage(b *strings.Builder, errMsg, indent string) {
b.WriteString(indent)
if !strings.HasPrefix(errMsg, listPoint) {
b.WriteString(listPoint)
}
// Indent the whole error message.
errMsg = strings.ReplaceAll(errMsg, "\n", "\n"+indent)
b.WriteString(errMsg)
}

var newLineReplacer = strings.NewReplacer("\n", "\\n", "\r", "\\r")

// PropertyValueString returns the string representation of the given value.
// Structs, interfaces, maps and slices are converted to compacted JSON strings.
// It tries to improve readability by:
// - limiting the string to 100 characters
// - removing leading and trailing whitespaces
// - escaping newlines
// If value is a struct implementing [fmt.Stringer] String method will be used
// only if the struct does not contain any JSON tags.
func PropertyValueString(v interface{}) string {
if v == nil {
return ""
}
rv := reflect.ValueOf(v)
ft := reflect.Indirect(rv)
var s string
switch ft.Kind() {
case reflect.Interface, reflect.Map, reflect.Slice:
if reflect.ValueOf(v).IsZero() {
break
}
raw, _ := json.Marshal(v)
s = string(raw)
case reflect.Struct:
if reflect.ValueOf(v).IsZero() {
break
}
if stringer, ok := v.(fmt.Stringer); ok && !hasJSONTags(v, rv.Kind() == reflect.Pointer) {
s = stringer.String()
break
}
raw, _ := json.Marshal(v)
s = string(raw)
case reflect.Invalid:
return ""
default:
s = fmt.Sprint(ft.Interface())
}
s = limitString(s, 100)
s = strings.TrimSpace(s)
s = newLineReplacer.Replace(s)
return s
}

func hasJSONTags(v interface{}, isPointer bool) bool {
t := reflect.TypeOf(v)
if isPointer {
t = t.Elem()
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if _, hasTag := field.Tag.Lookup("json"); hasTag {
return true
}
}
return false
}

func limitString(s string, limit int) string {
if len(s) > limit {
return s[:limit] + "..."
}
return s
}
13 changes: 13 additions & 0 deletions internal/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package internal

import "reflect"

const RequiredErrorMessage = "property is required but was empty"

const RequiredErrorCodeString = "required"

// IsEmptyFunc verifies if the value is zero value of its type.
func IsEmptyFunc(v interface{}) bool {
rv := reflect.ValueOf(v)
return rv.Kind() == 0 || rv.IsZero()
}
2 changes: 1 addition & 1 deletion pkg/govy/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Validation

Package validation implements a functional API for consistent,
package govy implements a functional API for consistent,
type safe validation.
It puts heavy focus on end user errors readability,
providing means of crafting clear and information-rich error messages.
Expand Down
2 changes: 1 addition & 1 deletion pkg/govy/cascade_mode.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validation
package govy

// CascadeMode defines how validation should behave when an error is encountered.
type CascadeMode uint
Expand Down
4 changes: 2 additions & 2 deletions pkg/govy/doc.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Package validation implements a functional API for consistent struct level validation.
package validation
// package govy implements a functional API for consistent struct level validation.
package govy
9 changes: 9 additions & 0 deletions pkg/govy/error_code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package govy

// ErrorCode is a unique string that represents a specific [RuleError].
// It can be used to precisely identify the error without inspecting its message.
type ErrorCode = string

const (
ErrorCodeTransform ErrorCode = "transform"
)
40 changes: 0 additions & 40 deletions pkg/govy/error_codes.go

This file was deleted.

Loading

0 comments on commit 320f5a5

Please sign in to comment.