structbind

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: MIT Imports: 4 Imported by: 0

README

StructBind

This project is a very simpler binder for structs in Golang.

Installation

go get github.com/quantumcycle/structbind

Example

package main

import (
	"net/http"
    "github.com/quantumcycle/structbind"
)

type MyInputStruct struct {
	Param1  int    `bind:"query=param1"`
	Header1 string `bind:"header=header1"`
}

func main() {
    b := structbind.NewBinder[*http.Request]()
    b.AddBinding("query", func(name string, req *http.Request) (any, error) {
        return req.URL.Query().Get(name), nil
    })
    b.AddBinding("header", func(name string, req *http.Request) (any, error) {
        h := req.Header.Get(name)
        if h == "" {
            return nil, nil
        }
        return h, nil
    })
    
    mockReq, _ := http.NewRequest("GET", "http://example.com?param1=23", nil)
    mockReq.Header.Set("header1", "test")
    
    result := MyInputStruct{
        Header1: "default value",
    }
    err = b.Bind(mockReq, &result)
	//...
}

Usage

The AddBinding adds a new type of binding. You can then use the name in the bind tag of the struct to make use of that binding.

When registering a binding, you must provide a function that receives a name argument and a source type. The function must return the value of the binding or an error. It can also return nil. In that case, the original value in the struct will stay untouched. If you return an empty string, the field will be set to to the type default value.

This lib is using github.com/mitchellh/mapstructure underneath so refer to it for more information.

Here is an example using Gorilla MUX to extract path and query parameters

b := structbind.NewBinder[*http.Request]()
b.AddBinding("path", func(name string, req *http.Request) (any, error) {
    vars := mux.Vars(req)
	return vars[name]
})
b.AddBinding("query", func(name string, req *http.Request) (any, error) {
    return req.URL.Query().Get(name), nil
})

Then you could use it on a struct like this to automatically inject values from the request

type CategoryListingParams struct {
    Category string `bind:"path=category"`
    Page     int    `bind:"query=page"`
}

mapstructure is doing some weak binding, so it will automatically convert the string query param page into an int. If the conversion fails, it will return an error.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Binder

type Binder[T any] struct {
	// contains filtered or unexported fields
}

func NewBinder

func NewBinder[T any]() *Binder[T]

func (*Binder[T]) AddBinding

func (b *Binder[T]) AddBinding(name string, fn Extractor[T]) *Binder[T]

func (*Binder[T]) Bind

func (b *Binder[T]) Bind(req T, target interface{}) error

type Extractor

type Extractor[T any] func(hint string, targetType reflect.Type, source T) (any, error)

Jump to

Keyboard shortcuts

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