structs

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

README

Structs

Structs contains various utilities to work with Go structs. It's basically a high level package based on primitives from the reflect package.

Feel free to add new functions or improve the existing code.

Install

go get github.com/iconimpact/go-core/structs

Usage and Examples

structs.Populate sets fields from 'b' into 'a' struct by matching name and type.

Options
  • a tag is in the form of: populate:"name"
  • use pointers in 'b' fields to pass empty values
  • use tag to match field from 'b' to 'a'
  • ignore fields from 'b' with populate:"-" tag
a := struct {
    Name string
    Description string
    Status int
    ID int
    FileName string
}{
    Name: "John",
    Description: "pointer in b, skip",
    Status: 10,
}

b := struct {
    Name *string
    Description *string
    Status int
    ID int `populate:"-"`
    Filename string `populate:"FileName"`
}{
    Name: "John Wick",
    ID: 20,
    Filename: "image.png",
}

err := structs.Populate(&a, b)

result:
{Name:"John Wick", Description:"pointer in b, skip", Status:0, ID: 0, FileName: "image.png"}

structs.Sanitize removes all leading and trailing white spaces from struct exported string fields.

Options
  • a tag is in the form of: sanitize:"option"
  • ignore fields with sanitize:"-" tag
  • clean and validate email with sanitize:"email" tag
a := struct {
    Name string
    Status int
    Password string `sanitize:"-"`
    Email string `sanitize:"email"`
}{
    Name: " John Wick ",
    Status: 10,
    Password: "my password ",
    Email: " email.string+me@googlemail.com",
}

err := structs.Sanitize(&a)

result:
{Name:"John Wick", Status:0, Password:"my password ", Email:"email.string+me@gmail.com"}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanEmail

func CleanEmail(str string) (string, error)

CleanEmail regexp cleans and validates string is an email address and all @googlemail.com addresses are normalized to @gmail.com.

func IsEmail

func IsEmail(email string) bool

IsEmail checks if an email is RFC 2821, 2822 compliant taken from http://regexlib.com/REDetails.aspx?regexp_id=2558 returns true if it is a valid email

func Populate

func Populate(a, b interface{}) error

Populate sets fields from 'b' into 'a' struct by matching name and type, use pointers in 'b' fields to pass empty values.

func Sanitize

func Sanitize(s interface{}) error

Sanitize removes all leading and trailing white spaces from struct exported string fields

Types

This section is empty.

Jump to

Keyboard shortcuts

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