Skip to content

michurin/tc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Type converters

build codecov Go Report Card Go Reference linting

Simple every day type conversion tooling.

Pointers

Thanks to modern golang and generics, you can say now:

pointerToOne = tc.P(1)

instead of using tons of typed casters like this.

There are different variants for this function: nil pointer for zero values, defaults... take a look at documentation

Types

There are functions to cat any to any type with or without fallback to default. Like this:

x := tc.DefCast(ctx.Value("key"), "default") // x = "default" if there is no "key" in context

or without default

x := tc.SafeCast[int](ctx.Value("key")) // x = 0 if there is no "key" in context

Ternary operator and similar things

Package provides sort of ternary operator:

// useful for globals
var messagePrefix = tc.Cmp(os.GetEnv("APP_ENVIRONMENT") == "prod", "", "[FROM STAGING] ")

and extended operator that deals with *bool, considering &true, &false and nil:

user := struct {
    Male *bool
}{}
gender := tc.CmpN(user.Male, "male", "female", "n/a")

Defaults for zero values

You can simply assume defaults for zero values like this:

var tmpDir = tc.DefZero(os.GetEnv("TMP"), "/tmp")

More details

Documentation.

Related resources

About

Golang very simple type converting tools

Resources

License

Stars

Watchers

Forks

Languages