Skip to content

Latest commit

 

History

History
65 lines (54 loc) · 1.53 KB

README.md

File metadata and controls

65 lines (54 loc) · 1.53 KB

ptr

Golang value <-> pointer utils.
Inspired by aws pointer utils.
The goal is to avoid using AWS pointer utilities for convenience, spreading AWS dependencies where they are not needed.

Example

Value -> Pointer

// before
someString := "foo"
someSDK.SomeMethod(&someString)

// after
someSDK.SomeMethod(ptr.To("foo"))
// or
someSDK.SomeMethod(ptr.String("foo"))

Pointer -> Value

// before
strPtr := someSDK.GetStringPtr()
var str string
if strPtr != nil {
  str = *strPtr
} 

// after
strPtr := someSDK.GetStringPtr()
str := ptr.Value(strPtr)
// or
str := ptr.StringValue(strPtr)

Generic core funcs

Core functions use generics To[T any](v T) *t and Value[T any](p *T) T. So you can easily use it with structs, slices and maps.

It also offers slice and maps transformations like the aws utility.

func ToSlice[T any](v []T) []*T
func ToMap[K comparable, T any](v map[K]T) map[K]*T
func ValueSlice[T any](p []*T) []T
func ValueMap[K comparable, T any](v map[K]*T) map[K]T

It offers out of the box type wrappers for:

  • string
  • byte
  • bool
  • int, int8, int16, int32 and int64
  • uint, uint8, uint16, uint32 and uint64
  • float32 and float64
  • time.Time

Development guide

For sake of avoiding writing the same lines of code for both ptr.go and ptr_test.go to create the type wrappers, I created the ./generator.go to generate the type boilerplate for both source and test.

To generaten run:

cd generator
go run main.go