Skip to content
/ henge Public

Henge is a type conversion library for Golang

License

Notifications You must be signed in to change notification settings

soranoba/henge

Folders and files

NameName
Last commit message
Last commit date
Oct 6, 2020
Jan 10, 2021
Apr 9, 2024
Jan 6, 2021
Apr 25, 2020
Nov 2, 2020
May 8, 2021
May 8, 2021
Nov 2, 2020
Apr 9, 2024
May 8, 2021
Nov 1, 2020
May 8, 2021
Nov 1, 2020
May 8, 2021
Nov 2, 2020
Apr 9, 2024
May 9, 2021
May 8, 2021
Apr 9, 2024
May 8, 2021
May 8, 2021
May 10, 2021
Jan 9, 2021
May 8, 2021
May 9, 2021
May 8, 2021
Jan 10, 2021
May 8, 2021
Nov 1, 2020
Apr 9, 2024
Nov 1, 2020
Nov 1, 2020
Nov 3, 2020
May 8, 2021
May 8, 2021
Apr 9, 2024

Repository files navigation

Henge

CircleCI Go Report Card PkgGoDev

Henge is a type conversion library for Golang.

変化 (Henge) means "Appearing with a different figure." in Japanese.
Henge as the name implies can easily convert to different types.

Overviews

  • 💫 Easily converting to various types
    • int64, uint64, float64, bool, string, slice, map, and struct.
  • ⚡ Simple and minimal code.
  • 🔧 Support for custom conversions by callbacks before and after conversion.

Motivations

Easily converting pointer and non-pointer types.

In Golang world, there is a trade-off between pointer and non-pointer type, so it is used both as needed.
For the reason, it is often necessary to convert between pointers and non-pointers.

When using Henge, it can easily convert even if it's a struct field.

Easily converting to a different struct.

There are many cases where the API server response is not the same as the DB record.
Henge is very useful if you just want to copy, but want to ignore some fields.

Easily create pointer-type values.

If we try to assign a non-zero constant value to a String or Int pointer type, we need to write codes of multiple lines.
When using Henge, it easy to create pointer types while preserving the benefits of types.

Installation

To install it, run:

go get -u github.com/soranoba/henge/v2

Usage

Conversion to the specified type.

import (
	"fmt"

	"github.com/soranoba/henge/v2"
)

func main() {
	type UserRequest struct {
		Name *string
		Age  *int
	}
	type User struct {
		Name string // *string to string
		Age  string // *int to string
	}

	name := "Alice"
	age := 30
	var user User
	if err := henge.New(UserRequest{Name: &name, Age: &age}).Convert(&user); err != nil {
		return
	}
	fmt.Printf("%#v", user)
}

Conversion by method chain.

import (
	"fmt"

	"github.com/soranoba/henge/v2"
)

func main() {
	i, err := henge.New("1.25").Float().Int().Result()
	if err != nil {
		return
	}
	// "1.25" -> 1.25 -> 1
	fmt.Println(i)
}