natools4go

module
v1.25.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2026 License: MIT

README ยถ

๐Ÿงฐ natools4go

A pragmatic, generic-first Go toolkit โ€” batteries included for building production applications with Fiber v3, GORM, Viper, and more.

Go Version License Coverage Go Report Card

Go ๅผ€ๅ‘่€…็š„ไธ€็ซ™ๅผๅทฅๅ…ท็ฎฑ ยท A one-stop toolkit for Go developers


๐Ÿ“ฆ Installation

go get -u github.com/natholdallas/natools4go

Requires Go 1.25+ (uses generics and modern stdlib features).


โœจ Highlights

๐Ÿ’ก Feature ๐Ÿ“š Where
Generic query builder orms.Query[T] โ€” fluent, type-safe, chainable
Generic DB models orms.SoftModel[T], orm.Model[T], IDModel[T]
Fiber request binding fext.BodyVarser[T] + automatic validation
Unified error response fext.ErrorHandler / fext.Fail
JWT middleware fext.Jwtware, fext.GenToken, fext.ParseToken
Config management vipers โ€” typed getters + hot-reload events
Structured validation va.Struct โ€” pretty, readable field errors

๐Ÿ—‚ Package Overview

Package Description Highlights
slice Generic slice utilities Map, Filter, ForEach, Defu
maths Integer math helpers DivCeil, Digits
strs String helpers & character constants Wrap, ToStart/ToEnd, Trim*, FormatInt/Uint/Float/Bool
rands Randomization & distribution Char, Pick, Distribute(Strict), Digits, FisherYateShuffle
jsons JSON marshal / nested-map traversal Unmarshal[T], IString, GetOK, Set
va Validator wrapper (go-playground/validator/v10) Struct, Var
constraints Generic type constraints Signed, Unsigned, Integer, Float
concur Concurrency primitives Go/Run, Pool (bounded), panic recovery
ask Interactive CLI prompts Read[T], Line, Confirm
spew Pretty debugging output JSON, Struct, Err, File
structs Map / struct conversion (mapstructure) Map, To[T], Vo[T] (validated)
flags CLI flag helpers Run
vipers Typed Viper config access with watch events Get[T], Watch, Reload, NewUpdateEvent
fext Fiber v3 extensions: binding, JWT, errors, cache, logging BodyVarser[T], Jwtware, ErrorHandler, Cache
orms GORM wrapper: models, pagination, sorting, typed queries Query[T], Paginate, SoftModel[T], LogPreset

๐Ÿš€ Quick Start

HTTP service with Fiber + GORM
package main

import (
	"github.com/gofiber/fiber/v3"
	"github.com/natholdallas/natools4go/fext"
	"github.com/natholdallas/natools4go/orms"
)

type User struct {
	orms.IDModel[int]
	Name string `json:"name" validate:"required"`
}

type CreateUser struct {
	Name string `json:"name" validate:"required"`
}

func main() {
	db := orms.New(postgres.Open("host=localhost user=postgres dbname=app"))
	db.AutoMigrate(&User{})

	app := fiber.New(fiber.Config{ErrorHandler: fext.ErrorHandler})

	app.Post("/users", func(c fiber.Ctx) error {
		body, err := fext.BodyVarser[CreateUser](c) // bind + validate
		if err != nil {
			return &fext.Fail{Status: 400, Message: "bad request", System: err}
		}
		user := User{Name: body.Name}
		if err := orms.Create(db, &user); err != nil {
			return err
		}
		return fext.JSON(c, 201, user)
	})

	fext.Listen(app, ":8080")
}
Typed, fluent queries
type User struct {
	orms.SoftModel[int]
	Email string
	Age   int
}

// Chainable builder, fully type-safe
q := orms.QE[User](db)
user, err := q.Where("age > ?", 18).Order("age desc").First()
// => (User, error)

list, err := orms.QE[User](db).
	Where("email LIKE ?", "%@example.com").
	Preload("Orders").
	Find()
// => ([]User, error)
Pagination โ€” no side effects, no boilerplate
page, tx := orms.Paginate[User](
	orms.QE[User](db).Where("active = ?", true),
	orms.Pagination{Page: 2, Size: 20},
)
// Page{Total: 137, Page: 7, Content: []User{...}}
JWT auth in three lines
app.Use(fext.Jwtware("my-secret")) // protects all following routes

token, _ := fext.GenToken("user-42", "my-secret") // 30d default
claims, err := fext.ParseToken(token, "my-secret") // HMAC enforced
Config with hot reload
vipers.Config("config", "./config") // config.toml by default
vipers.Watch(func(e fsnotify.Event) {
	log.Println("config changed:", e.Name)
})

port := vipers.Int("server.port", 8080)  // typed + default
debug := vipers.Bool("server.debug", false)
Validation errors you can actually read
type SignUp struct {
	Email string `validate:"required,email"`
	Age   int    `validate:"gte=18"`
}

err := va.Struct(SignUp{Email: "x"})
// [Email:x]:[required]
// [Age:0]:[gte-18]

๐Ÿงฉ Detailed Guides

orms โ€” GORM wrapper
  • Init: orms.New(dialector, opts...) opens the pool, panics with context on failure. Connection pool (max idle 10 / max open 100 / lifetime 30m) can be tuned afterwards.
  • DSN builder: orms.Dsn(driver, name, user, pass, host, port) and orms.Dialector(driver, dsn, name, query, prepare...) cover MySQL / PostgreSQL / SQLite / SQL Server / ClickHouse. prepare=true auto-creates the database.
  • Models: IDModel[T], Model[T], SoftModel[T] โ€” ready-made generic base models with swagger-compatible tags.
  • Query builder: orms.Q[T] (no model), orms.QE[T] (model = T), orms.QM[T,M] (result T, model M), orms.QT[T](db, table). Every chainable method on Query[T] mirrors GORM and stays type-safe. Finishers return (T, error) or []T, error; I-prefixed variants swallow errors and return zero values (IFind, IFirst, IPaginate โ€ฆ).
  • Pagination: Pagination.Scope is side-effect free (validates on a copy), Paginate/PaginateMapping give you Page[T]{Total, Page, Content}.
  • Sorting: Sorter / Sorters implement Scoper for safe column ordering.
  • JSON columns: List[T] / Dict[T] implement sql.Scanner + driver.Valuer for JSON persistence across databases.
  • Logging: orms.LogPreset(out, level) builds a stdlib-backed GORM logger.
fext โ€” Fiber v3 extensions
  • Binding: BodyParser, BodyVarser, QueryParser/Varser, ParamsParser/Varser, RestParser/Varser, CookieParser/Varser, ReqHeaderParser/Varser, FormData. The Varser suffix = bind + validate (va.Struct). Structs may implement Initializer to run after binding.
  • Errors: fext.Fail{Status, Code, Message, System} โ€” returns a clean JSON body. ErrorHandler maps *Fail, *fiber.Error, and generic errors to proper status codes; System is surfaced only in debug mode (SetDebugMode(true)) and forwarded to SetErrorFunc.
  • JWT: Jwtware (HS256 middleware), GenToken / ParseToken (algorithm-confusion safe โ€” only HMAC accepted), Jwt struct for grouped secret management, Jwt.Claims(c) to read the current user.
  • Utils: Cache(seconds), Status/JSON/SendString, GetAuthorization, FmtPort, SetLogLevel, and Listen (config-aware, nil-safe error callback).
vipers โ€” typed config

Every getter accepts an optional default: Get[T], String, Bool, Int*, Uint*, Float64, Time, Duration, IntSlice, StringSlice, StringMap*, SizeInBytes. Watch subscribes to file changes; handlers registered via NewUpdateEvent are deduplicated and dispatched under a lock (Reload).

concur โ€” concurrency done right
  • Go(tasks...) / Run(tasks...) โ€” fan out and join.
  • Pool{workers} โ€” bounded concurrency via NewPool(n), Submit, Wait, Close. Submitting after Close panics to catch misuse.
  • Panics in tasks are recovered and reported through SetPanicHandler (default: re-panic after capturing the stack).
jsons โ€” JSON helpers

Unmarshal[T], Marshal(v, pretty?), String(v, pretty?), plus I-variants that ignore errors. Map(v) converts any value to map[string]any. Get/Set/GetOK traverse nested maps without panicking on missing keys.

spew โ€” debug printing

JSON, Struct, Err, Dump, and File (cat-like). Customize the printer globally with SetPrinter to route output to your logger instead of stdout.


๐Ÿงช Testing

The core pure-function packages (slice, maths, strs, rands, jsons, concur) ship with table-driven tests:

go test ./...      # run all tests
go test -race ./... # race detector for concurrent packages
go vet ./...

๐Ÿงญ Project Layout

natools4go/
โ”œโ”€โ”€ ask/          # interactive CLI prompts
โ”œโ”€โ”€ concur/       # goroutines, bounded pool, panic recovery
โ”œโ”€โ”€ constraints/  # generic type constraints
โ”œโ”€โ”€ fext/         # Fiber v3: binders, JWT, error handling, cache
โ”œโ”€โ”€ flags/        # CLI flag helpers
โ”œโ”€โ”€ jsons/        # JSON marshal + nested map helpers
โ”œโ”€โ”€ maths/        # integer math
โ”œโ”€โ”€ orms/         # GORM: models, query builder, pagination
โ”œโ”€โ”€ rands/        # random strings, distribution, shuffle
โ”œโ”€โ”€ slice/        # slice utilities
โ”œโ”€โ”€ spew/         # pretty debugging
โ”œโ”€โ”€ strs/         # string helpers & constants
โ”œโ”€โ”€ structs/      # mapstructure conversion
โ”œโ”€โ”€ va/           # validator wrapper
โ””โ”€โ”€ vipers/       # typed Viper config + hot reload

๐Ÿ“„ License

MIT ยฉ natools4go contributors.

Directories ยถ

Path Synopsis
Package ask is tiny packaging support fmt
Package ask is tiny packaging support fmt
Package concur provides primitives for managing concurrent execution of tasks.
Package concur provides primitives for managing concurrent execution of tasks.
Package constraints defines a set of useful constraints to be used with type parameters.
Package constraints defines a set of useful constraints to be used with type parameters.
Package fext is tiny packaging support fiber
Package fext is tiny packaging support fiber
Package flags provides a set of functions to parse command line arguments
Package flags provides a set of functions to parse command line arguments
Package jsons is tiny packaging support json
Package jsons is tiny packaging support json
Package maths
Package maths
Package orms provides advanced utilities for GORM, including generic models, automated pagination, dynamic sorting, and a fluent query builder.
Package orms provides advanced utilities for GORM, including generic models, automated pagination, dynamic sorting, and a fluent query builder.
Package rands
Package rands
Package redisx is a tiny package for Redis client
Package redisx is a tiny package for Redis client
Package slice is tiny packaging support slice
Package slice is tiny packaging support slice
Package spew provides high-visibility debugging and file inspection tools.
Package spew provides high-visibility debugging and file inspection tools.
Package strs
Package strs
Package structs is tiny packaging support structs
Package structs is tiny packaging support structs
Package va is tiny packaging support validator
Package va is tiny packaging support validator
Package vipers is tiny packaging support viper
Package vipers is tiny packaging support viper

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL