Skip to content

Commit e640e1e

Browse files
Merge pull request #298 from pace/add-helpers
http,runtime: Add useful default sanitizers
2 parents 190f840 + eed9252 commit e640e1e

File tree

20 files changed

+892
-95
lines changed

20 files changed

+892
-95
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ require (
3333
github.com/prometheus/client_golang v1.7.1
3434
github.com/rs/xid v1.2.1
3535
github.com/rs/zerolog v1.17.2
36+
github.com/satori/go.uuid v1.2.0
3637
github.com/shopspring/decimal v0.0.0-20200105231215-408a2507e114
3738
github.com/sony/gobreaker v0.4.1
3839
github.com/spf13/cobra v1.2.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,8 @@ github.com/ryanrolds/sqlclosecheck v0.3.0/go.mod h1:1gREqxyTGR3lVtpngyFo3hZAgk0K
678678
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
679679
github.com/sanposhiho/wastedassign/v2 v2.0.6 h1:+6/hQIHKNJAUixEj6EmOngGIisyeI+T3335lYTyxRoA=
680680
github.com/sanposhiho/wastedassign/v2 v2.0.6/go.mod h1:KyZ0MWTwxxBmfwn33zh3k1dmsbF2ud9pAAGfoLfjhtI=
681+
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
682+
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
681683
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
682684
github.com/securego/gosec/v2 v2.8.1 h1:Tyy/nsH39TYCOkqf5HAgRE+7B5D8sHDwPdXRgFWokh8=
683685
github.com/securego/gosec/v2 v2.8.1/go.mod h1:pUmsq6+VyFEElJMUX+QB3p3LWNHXg1R3xh2ssVJPs8Q=
File renamed without changes.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright © 2021 by PACE Telematics GmbH. All rights reserved.
2+
// Created at 2021/02/04 by Julius Foitzik
3+
4+
package runtime
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"strconv"
10+
11+
uuid "github.com/satori/go.uuid"
12+
"github.com/shopspring/decimal"
13+
14+
"github.com/pace/bricks/pkg/isotime"
15+
)
16+
17+
var _ ValueSanitizer = (*datetimeSanitizer)(nil)
18+
var _ ValueSanitizer = (*intSanitizer)(nil)
19+
var _ ValueSanitizer = (*decimalSanitizer)(nil)
20+
var _ ValueSanitizer = (*noopSanitizer)(nil)
21+
var _ ValueSanitizer = (*uuidSanitizer)(nil)
22+
var _ ValueSanitizer = (*composableAndFieldRestrictedSanitizer)(nil)
23+
24+
var (
25+
ErrInvalidFieldname = errors.New("invalid fieldName, not registered in sanitizer")
26+
)
27+
28+
type datetimeSanitizer struct{}
29+
30+
func (d datetimeSanitizer) SanitizeValue(fieldName string, value string) (interface{}, error) {
31+
t, err := isotime.ParseISO8601(value)
32+
if err != nil {
33+
return nil, err
34+
}
35+
return t, nil
36+
}
37+
38+
type intSanitizer struct{}
39+
40+
func (i intSanitizer) SanitizeValue(fieldName string, value string) (interface{}, error) {
41+
return strconv.Atoi(value)
42+
}
43+
44+
type decimalSanitizer struct{}
45+
46+
func (d decimalSanitizer) SanitizeValue(fieldName string, value string) (interface{}, error) {
47+
return decimal.NewFromString(value)
48+
}
49+
50+
type noopSanitizer struct{}
51+
52+
func (n noopSanitizer) SanitizeValue(fieldName string, value string) (interface{}, error) {
53+
return value, nil
54+
}
55+
56+
type uuidSanitizer struct{}
57+
58+
func (u uuidSanitizer) SanitizeValue(fieldName string, value string) (interface{}, error) {
59+
if _, err := uuid.FromString(value); err != nil {
60+
return nil, err
61+
}
62+
return value, nil
63+
}
64+
65+
type composableAndFieldRestrictedSanitizer map[string]ValueSanitizer
66+
67+
func (c composableAndFieldRestrictedSanitizer) SanitizeValue(fieldName string, value string) (interface{}, error) {
68+
san, found := c[fieldName]
69+
if !found {
70+
return nil, fmt.Errorf("%w: %v", ErrInvalidFieldname, fieldName)
71+
}
72+
return san.SanitizeValue(fieldName, value)
73+
}
74+
75+
func NewComposableSanitizer(mapping map[string]ValueSanitizer) ValueSanitizer {
76+
return composableAndFieldRestrictedSanitizer(mapping)
77+
}
78+
79+
func NewDatetimeSanitizer() ValueSanitizer {
80+
return &datetimeSanitizer{}
81+
}
82+
83+
func NewIntSanitizer() ValueSanitizer {
84+
return &intSanitizer{}
85+
}
86+
func NewNoopSanitizer() ValueSanitizer {
87+
return &noopSanitizer{}
88+
}
89+
func NewUUIDSanitizer() ValueSanitizer {
90+
return &uuidSanitizer{}
91+
}
92+
func NewDecimalSanitizer() ValueSanitizer {
93+
return &decimalSanitizer{}
94+
}

vendor/github.com/caarlos0/env/.golangci.yml

Lines changed: 0 additions & 8 deletions
This file was deleted.

vendor/github.com/caarlos0/env/.goreleaser.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

vendor/github.com/caarlos0/env/Makefile

Lines changed: 0 additions & 37 deletions
This file was deleted.

vendor/github.com/caarlos0/env/env_unix.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

vendor/github.com/caarlos0/env/env_windows.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)