-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2c78d6
commit 60c6a93
Showing
6 changed files
with
206 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Go CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: "^1.22" # Specify Go version here | ||
|
||
- name: Install dependencies | ||
run: go mod tidy | ||
|
||
- name: Run unit tests | ||
run: go test ./tests -v | ||
|
||
- name: Run benchmarks | ||
run: go test -benchmem -run=^$ -bench . ./benchmarks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,60 @@ | ||
package zod | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type Schema interface { | ||
Validate(data interface{}) error | ||
} | ||
|
||
type Validator func(data interface{}) error | ||
|
||
func ValidateSchema(schema Schema, data interface{}) error { | ||
return schema.Validate(data) | ||
} | ||
|
||
type Result struct { | ||
IsValid bool | ||
Error error | ||
} | ||
|
||
func Validator(schema Schema, data interface{}, results chan<- Result, wg *sync.WaitGroup) { | ||
defer wg.Done() | ||
|
||
err := schema.Validate(data) | ||
|
||
var isValid bool | ||
if err == nil { | ||
isValid = true | ||
} else { | ||
isValid = false | ||
} | ||
|
||
results <- Result{IsValid: isValid, Error: err} | ||
} | ||
|
||
func ValidateConcurrently(schema Schema, dataList []interface{}, workerCount int) []Result { | ||
|
||
results := make(chan Result, len(dataList)) | ||
var wg sync.WaitGroup | ||
|
||
sem := make(chan struct{}, workerCount) | ||
|
||
for _, data := range dataList { | ||
wg.Add(1) | ||
go func(data interface{}) { | ||
sem <- struct{}{} | ||
defer func() { <-sem }() | ||
Validator(schema, data, results, &wg) | ||
}(data) | ||
} | ||
|
||
wg.Wait() | ||
close(results) | ||
|
||
var validationResults []Result | ||
for result := range results { | ||
validationResults = append(validationResults, result) | ||
} | ||
|
||
return validationResults | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters