Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Nov 16, 2024
0 parents commit 84fb157
Show file tree
Hide file tree
Showing 75 changed files with 6,715 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .editorconfig
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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
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
21 changes: 21 additions & 0 deletions .gitignore
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
21 changes: 21 additions & 0 deletions LICENSE
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.
30 changes: 30 additions & 0 deletions Makefile
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
31 changes: 31 additions & 0 deletions README.md
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
```
33 changes: 33 additions & 0 deletions assert/README.md
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)
146 changes: 146 additions & 0 deletions assert/any.go
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
}
Loading

0 comments on commit 84fb157

Please sign in to comment.