-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 84fb157
Showing
75 changed files
with
6,715 additions
and
0 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,22 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[{*.go,Makefile,.gitmodules,go.mod,go.sum}] | ||
indent_style = tab | ||
|
||
[*.md] | ||
indent_style = tab | ||
trim_trailing_whitespace = false | ||
|
||
[*.{yml,yaml,json}] | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
[*.{js,jsx,ts,tsx,css,less,sass,scss,vue,py}] | ||
indent_style = space | ||
indent_size = 4 |
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,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
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,38 @@ | ||
name: Build & Test | ||
on: | ||
workflow_dispatch: | ||
push: | ||
paths: | ||
- '**/*.go' | ||
pull_request: | ||
paths: | ||
- '**/*.go' | ||
|
||
jobs: | ||
build: | ||
name: Build And Test | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, windows-latest, macos-latest] | ||
go: [1.22] | ||
steps: | ||
- name: Setup Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: ${{ matrix.go }} | ||
cache: false | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
with: | ||
submodules: true | ||
- name: Build | ||
run: make build | ||
- name: Test | ||
run: make test | ||
- name: Upload Coverage | ||
uses: codecov/[email protected] | ||
with: | ||
token: ${{ secrets.CODECOV_TOKEN }} | ||
slug: aacebo/assert | ||
files: converage.out |
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,21 @@ | ||
# If you prefer the allow list template instead of the deny list, see community template: | ||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore | ||
# | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Go workspace file | ||
go.work |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 aacebo | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,30 @@ | ||
clean: | ||
rm -rf ./bin | ||
rm coverage.out | ||
|
||
fmt: | ||
gofmt -w ./ | ||
|
||
doc: | ||
godoc -http=:6060 | ||
|
||
test: | ||
go clean -testcache | ||
go test ./... -cover -coverprofile=coverage.out | ||
|
||
test.v: | ||
go clean -testcache | ||
go test ./... -cover -coverprofile=coverage.out -v | ||
|
||
test.cov: | ||
go tool cover -html=coverage.out | ||
|
||
test.bench: | ||
go clean -testcache | ||
go test -bench=. -benchmem | ||
|
||
test.bench.profile: | ||
go clean -testcache | ||
go test -bench=. -benchmem -memprofile memory.out | ||
|
||
.PHONY: test |
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 @@ | ||
<p align="center"> | ||
<img src="./assets/icon.png" width="120px" style="border-radius:20%" /> | ||
</p> | ||
|
||
<p align="center"> | ||
a zero dependency performant toolset for making apis | ||
</p> | ||
|
||
<p align="center"> | ||
<a href="https://opensource.org/licenses/MIT" target="_blank" alt="License"> | ||
<img src="https://img.shields.io/badge/License-MIT-blue.svg" /> | ||
</a> | ||
<a href="https://pkg.go.dev/github.com/thegogod/rum/gq" target="_blank" alt="Go Reference"> | ||
<img src="https://pkg.go.dev/badge/github.com/thegogod/rum/gq.svg" /> | ||
</a> | ||
<a href="https://goreportcard.com/report/github.com/thegogod/rum/gq" target="_blank" alt="Go Report Card"> | ||
<img src="https://goreportcard.com/badge/github.com/thegogod/rum/gq" /> | ||
</a> | ||
<a href="https://github.com/thegogod/rum/actions/workflows/ci.yml" target="_blank" alt="Build"> | ||
<img src="https://github.com/thegogod/rum/actions/workflows/ci.yml/badge.svg?branch=main" /> | ||
</a> | ||
<a href="https://codecov.io/gh/thegogod/rum"> | ||
<img src="https://codecov.io/gh/thegogod/rum/graph/badge.svg?token=9XETRUUQUY" /> | ||
</a> | ||
</p> | ||
|
||
# Install | ||
|
||
```bash | ||
go get github.com/thegogod/rum | ||
``` |
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,33 @@ | ||
# Assert | ||
|
||
a schema validator | ||
|
||
# Usage | ||
|
||
```go | ||
schema := assert.String().Required() | ||
|
||
if err := schema.Validate("..."); err != nil { // nil | ||
panic(err) | ||
} | ||
``` | ||
|
||
# Features | ||
|
||
| Name | Status | | ||
|----------------------------|---------------------| | ||
| Any | ✅ | | ||
| Bool | ✅ | | ||
| Float | ✅ | | ||
| Int | ✅ | | ||
| String | ✅ | | ||
| Object | ✅ | | ||
| Array | ✅ | | ||
| Time | ✅ | | ||
| Union | ✅ | | ||
| Custom Error Messages | ✅ | | ||
| Custom Rules | ✅ | | ||
|
||
# Benchmarks | ||
|
||
![Benchmarks](./../assets/assert/benchmarks.png) |
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,146 @@ | ||
package assert | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"slices" | ||
) | ||
|
||
type AnySchema []Rule | ||
|
||
func Any() *AnySchema { | ||
self := &AnySchema{} | ||
self.Rule("type", self.Type(), func(value reflect.Value) (any, error) { | ||
if !value.IsValid() { | ||
return nil, nil | ||
} | ||
|
||
return value.Interface(), nil | ||
}) | ||
|
||
return self | ||
} | ||
|
||
func (self AnySchema) Type() string { | ||
return "any" | ||
} | ||
|
||
func (self *AnySchema) Rule(key string, value any, rule RuleFn) *AnySchema { | ||
i := slices.IndexFunc(*self, func(rule Rule) bool { | ||
return rule.Key == key | ||
}) | ||
|
||
if i > -1 { | ||
(*self)[i] = Rule{ | ||
Key: key, | ||
Value: value, | ||
Resolve: rule, | ||
} | ||
} else { | ||
*self = append(*self, Rule{ | ||
Key: key, | ||
Value: value, | ||
Resolve: rule, | ||
}) | ||
} | ||
|
||
return self | ||
} | ||
|
||
func (self *AnySchema) Message(message string) *AnySchema { | ||
(*self)[len(*self)-1].Message = message | ||
return self | ||
} | ||
|
||
func (self *AnySchema) Required() *AnySchema { | ||
return self.Rule("required", true, func(value reflect.Value) (any, error) { | ||
if !value.IsValid() { | ||
return nil, errors.New("required") | ||
} | ||
|
||
return value.Interface(), nil | ||
}) | ||
} | ||
|
||
func (self *AnySchema) Enum(values ...any) *AnySchema { | ||
return self.Rule("enum", values, func(value reflect.Value) (any, error) { | ||
if !value.IsValid() { | ||
return nil, nil | ||
} | ||
|
||
for _, v := range values { | ||
if value.Equal(reflect.Indirect(reflect.ValueOf(v))) { | ||
return value.Interface(), nil | ||
} | ||
} | ||
|
||
return nil, fmt.Errorf("must be one of %v", values) | ||
}) | ||
} | ||
|
||
func (self AnySchema) MarshalJSON() ([]byte, error) { | ||
buf := &bytes.Buffer{} | ||
buf.Write([]byte{'{'}) | ||
|
||
for i, item := range self { | ||
b, err := json.Marshal(item.Value) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
buf.WriteString(fmt.Sprintf("%q:", fmt.Sprintf("%v", item.Key))) | ||
buf.Write(b) | ||
|
||
if i < len(self)-1 { | ||
buf.Write([]byte{','}) | ||
} | ||
} | ||
|
||
buf.Write([]byte{'}'}) | ||
return buf.Bytes(), nil | ||
} | ||
|
||
func (self AnySchema) Validate(value any) error { | ||
return self.validate("", reflect.Indirect(reflect.ValueOf(value))) | ||
} | ||
|
||
func (self AnySchema) validate(key string, value reflect.Value) error { | ||
err := NewEmptyError("", key) | ||
|
||
for _, rule := range self { | ||
if rule.Resolve == nil { | ||
continue | ||
} | ||
|
||
v, e := rule.Resolve(value) | ||
|
||
if e != nil { | ||
if group, ok := e.(ErrorGroup); ok { | ||
for _, subErr := range group { | ||
err = err.Add(subErr) | ||
} | ||
} else { | ||
message := e.Error() | ||
|
||
if rule.Message != "" { | ||
message = rule.Message | ||
} | ||
|
||
err = err.Add(NewError(rule.Key, key, message)) | ||
continue | ||
} | ||
} | ||
|
||
value = reflect.ValueOf(v) | ||
} | ||
|
||
if len(err.Errors) > 0 { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.