go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
17k stars 1.33k forks source link

Use sync.OnceValue for lazy regexp initialization #1279

Open kke opened 5 months ago

kke commented 5 months ago

Fixes Or Enhances

Replaces the local lazy initialization implementation with the stdlib sync.OnceValue.

Using sync.OnceValue instead of the local implementation brings some benefits in allocations, panic handling and mutex usage after initialization.

sync.OnceValue was introduced in go1.21 so backwards compatibility is provided via lazy_compat.go and build tags.

Make sure that you've checked the boxes below before you submit PR:

@go-playground/validator-maintainers

kke commented 5 months ago

sync.OnceValue was introduced in go1.21, so there has to be two files with buildtags as validator supports older go versions.

coveralls commented 5 months ago

Coverage Status

coverage: 74.285% (-0.006%) from 74.291% when pulling 2c1e6659ad625ced14afa3798bdad7f2c61d7d38 on kke:sync-oncevalue-regex into a947377040f8ebaee09f20d09a745ec369396793 on go-playground:master.

coveralls commented 5 months ago

Coverage Status

coverage: 74.285% (-0.006%) from 74.291% when pulling 61e16c9e89e13580119bae96e686f5888068ecf8 on kke:sync-oncevalue-regex into a947377040f8ebaee09f20d09a745ec369396793 on go-playground:master.

coveralls commented 5 months ago

Coverage Status

coverage: 74.285% (-0.006%) from 74.291% when pulling dac5fe65522bc02695d7cacf05ecaca6e2fb9a85 on kke:sync-oncevalue-regex into a947377040f8ebaee09f20d09a745ec369396793 on go-playground:master.

kke commented 5 months ago

coverage: 74.285% (-0.006%) from 74.291%

The coverage check does not seem to calculate correctly when files with build tags are involved.