Skip to content

THRIFT-6012: Precompile regex patterns in Go struct validator#3495

Merged
kpumuk merged 1 commit into
apache:masterfrom
erka:go-validation-pattern
May 20, 2026
Merged

THRIFT-6012: Precompile regex patterns in Go struct validator#3495
kpumuk merged 1 commit into
apache:masterfrom
erka:go-validation-pattern

Conversation

@erka
Copy link
Copy Markdown
Contributor

@erka erka commented May 19, 2026

Client: go

Replace inline regexp.MatchString calls with precompiled
regexp.MustCompile variables for literal vt.pattern annotations.
This avoids recompiling the same regex on every Validate() call.

Also fixes two bugs in the original regexp.MatchString usage:

  • Arguments were swapped: MatchString(target, pattern) instead
    of MatchString(pattern, target)
  • Used ; ok instead of ; !ok, causing validation to fail when the
    pattern matched instead of when it didn't

Example

struct StarSystem {
  1: string id ( vt.pattern = "^[a-z_-]+$", vt.pattern = ".*")
  2: string hash (vt.pattern = "$id")
}

Output

// Precompiled regex patterns for StarSystem vt.pattern validation
var (
	vtReStarSystem0 = regexp.MustCompile(`^[a-z_-]+$`)
	vtReStarSystem1 = regexp.MustCompile(`.*`)
)

func (p *StarSystem) Validate() error {
	if !vtReStarSystem0.MatchString(p.ID){
		return thrift.NewValidationException(thrift.VALIDATION_FAILED, "vt.pattern", "StarSystem.id", "StarSystem.id not valid, rule vt.pattern check failed")
	}
	if !vtReStarSystem1.MatchString(p.ID){
		return thrift.NewValidationException(thrift.VALIDATION_FAILED, "vt.pattern", "StarSystem.id", "StarSystem.id not valid, rule vt.pattern check failed")
	}
	if ok, _ := regexp.MatchString(string(p.ID), p.Hash); !ok {
		return thrift.NewValidationException(thrift.VALIDATION_FAILED, "vt.pattern", "StarSystem.hash", "StarSystem.hash not valid, rule vt.pattern check failed")
	}
	return nil
}

@mergeable mergeable Bot added the compiler label May 19, 2026
@kpumuk kpumuk added the golang Pull requests that update Go code label May 19, 2026
@erka erka force-pushed the go-validation-pattern branch 3 times, most recently from cb5f767 to 8213771 Compare May 19, 2026 22:04
… patterns in Go validator

Client: go

Replace inline regexp.MatchString calls with precompiled
regexp.MustCompile variables for literal vt.pattern annotations.
This avoids recompiling the same regex on every Validate() call.

Also fixes two bugs in the original regexp.MatchString usage:
- Arguments were swapped: MatchString(target, pattern) instead
  of MatchString(pattern, target)
- Used ; ok instead of ; !ok, causing validation to fail when the
  pattern matched instead of when it didn't
@erka erka force-pushed the go-validation-pattern branch from 8213771 to 78362af Compare May 19, 2026 22:19
@erka erka changed the title Precompile regex patterns in Go struct validator THRIFT-6012: Precompile regex patterns in Go struct validator May 19, 2026
@erka erka marked this pull request as ready for review May 19, 2026 22:22
@erka erka requested a review from fishy as a code owner May 19, 2026 22:22
Copy link
Copy Markdown
Member

@fishy fishy left a comment

Choose a reason for hiding this comment

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

looks good to me. the lib-netstd failure doesn't seem relevant. cc @Jens-G to double check on that one.

@kpumuk kpumuk merged commit cf88b72 into apache:master May 20, 2026
179 of 182 checks passed
@kpumuk
Copy link
Copy Markdown
Member

kpumuk commented May 20, 2026

looks good to me. the lib-netstd failure doesn't seem relevant. cc @Jens-G to double check on that one.

We have merged a fix for that #3501

@erka erka deleted the go-validation-pattern branch May 21, 2026 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler golang Pull requests that update Go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants