/structs

Go Libraries that helps to play around with structs

Primary LanguageGoMIT LicenseMIT

Structs GoDoc CircleCI Coverage Status

This library helps you to play around with structs, such as bind incoming request into struct, validate struct, etc.

import "github.com/alileza/structs"

func main(){
    // Bind request value into struct
    err := structs.BindRequest(httpRequest, &target)
    
    // Validate struct value
    err := structs.ValidateStruct(&MyStruct)
    
    // Convert struct to map
    MyMap := structs.ToMap(MyStruct)
    
    // Copy struct/slice value to another
    err := structs.Copy(MyStruct, &MyOtherStruct)
}

Bind Request

BindRequest will scan your struct and bind the request Values / Body into your struct according to json tag on struct.

Example Here

Validate Struct

ValidateStruct will validate struct if required tag is equal to true.

Example Here

To Map

ToMap returns map following the input struct.

Example Here

Copy

Copy struct/slice/etc value to another