Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Switch to govy for validation #527

Merged
merged 6 commits into from
Aug 29, 2024
Merged

Conversation

nieomylnieja
Copy link
Collaborator

Motivation

Code previously located at internal/validation is now being open-sourced.
The new library govy comes with some initial changes to the structure of the code and the API.

Summary

Most of the changes were done via the following script, a few manual steps were required as well.
I've also removed unnecessary type annotations while at it.

#! /usr/bin/env bash

old_import="github.com\/nobl9\/nobl9-go\/internal\/validation"
new_import="github.com\/nobl9\/govy\/pkg\/govy"

# List of functions to transfer from govy to rules package generated by:
# rg -N -I 'func.*govy\.Rule' -g '!*_test.go' | \
#   awk '/^func [A-Z]/ {sub(/[\[\(].*/, "", $2); print $2}'

while read -r file; do
  sed -r -i '
  s/'"$old_import"'/'"$new_import"'/g;
  s/validation\./govy\./g;
  s/NewSingleRule/NewRule/g;
  s/SingleRule/Rule/g;
  s/govy\.StringLength/rules\.StringLength/g;
  s/govy\.StringMinLength/rules\.StringMinLength/g;
  s/govy\.StringMaxLength/rules\.StringMaxLength/g;
  s/govy\.SliceLength/rules\.SliceLength/g;
  s/govy\.SliceMinLength/rules\.SliceMinLength/g;
  s/govy\.SliceMaxLength/rules\.SliceMaxLength/g;
  s/govy\.MapLength/rules\.MapLength/g;
  s/govy\.MapMinLength/rules\.MapMinLength/g;
  s/govy\.MapMaxLength/rules\.MapMaxLength/g;
  s/govy\.EqualTo/rules\.EQ/g;
  s/govy\.NotEqualTo/rules\.NEQ/g;
  s/govy\.GreaterThan([\(\[])/rules\.GT\1/g;
  s/govy\.GreaterThanOrEqualTo/rules\.GTE/g;
  s/govy\.LessThan([\(\[])/rules\.LT\1/g;
  s/govy\.LessThanOrEqualTo/rules\.LTE/g;
  s/govy\.DurationPrecision/rules\.DurationPrecision/g;
  s/govy\.SliceUnique/rules\.SliceUnique/g;
  s/govy\.OneOf/rules\.OneOf/g;
  s/govy\.MutuallyExclusive/rules\.MutuallyExclusive/g;
  s/govy\.URL/rules\.URL/g;
  s/govy\.Forbidden/rules\.Forbidden/g;
  s/govy\.StringNotEmpty/rules\.StringNotEmpty/g;
  s/govy\.StringMatchRegexp/rules\.StringMatchRegexp/g;
  s/govy\.StringDenyRegexp/rules\.StringDenyRegexp/g;
  s/govy\.StringIsDNSSubdomain/rules\.StringDNSLabel/g;
  s/govy\.StringUUID/rules\.StringUUID/g;
  s/govy\.StringASCII/rules\.StringASCII/g;
  s/govy\.StringURL/rules\.StringURL/g;
  s/govy\.StringJSON/rules\.StringJSON/g;
  s/govy\.StringContains/rules\.StringContains/g;
  s/govy\.StringStartsWith/rules\.StringStartsWith/g;
  s/govy\.StringEndsWith/rules\.StringEndsWith/g;
  s/govy\.StringIsTitle/rules\.StringIsTitle/g;
  s/govy\.Required/rules\.Required/g;
  s/govy\.SelfHashFunc/rules\.HashFuncSelf/g;
  s/govy\.ErrorCode /rules\.ErrorCode /g;
  s/govy\.ErrorCode(\w+)/rules\.ErrorCode\1/g;
  s/rules\.ErrorCodeStringDescription/validationV1Alpha\.ErrorCodeStringDescription/g;
  s/rules\.ErrorCodeSeparator/govy\.ErrorCodeSeparator/g;
  s/rules\.ErrorCodeTransform/govy\.ErrorCodeTransform/g;
  s/govy\.JoinErrors/errorutils\.JoinErrors/g;
  s/govy\.StringDescription/validationV1Alpha\.StringDescription/g;
  s/rules\.ErrorCodeStringIsDNSSubdomain/rules\.ErrorCodeStringDNSLabel/g;
  ' "$file"
done < <(rg -l "$old_import" -g '!internal/validation/**' -g '!go.mod' -g '!go.sum')

# Followed by:
#  go mod tidy
#  make format
#  go vet ./...

Release Notes

nobl9-go is switching to a brand new validation library, govy.
The library was previously part of nobl9-go, but is now being open sourced! Go check it out ;)

@n9-machine-user n9-machine-user added enhancement New feature or request go minor labels Aug 21, 2024
@nieomylnieja nieomylnieja marked this pull request as ready for review August 28, 2024 15:05
Copy link
Member

@BSski BSski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, imports formatting should be corrected in the future. LGTM besides that.

@nieomylnieja nieomylnieja merged commit d350fc5 into main Aug 29, 2024
5 checks passed
@nieomylnieja nieomylnieja deleted the switch-to-govy-validation branch August 29, 2024 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request go minor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants