ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - kak-tus/nan: Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmarshallers
Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmarshallers - kak-tus/nan
Visit Site

GitHub - kak-tus/nan: Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmarshallers

GitHub - kak-tus/nan: Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmarshallers

nan - No Allocations Nevermore

Package nan - Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmarshallers.

Godoc Coverage Status Go Report Card Go

Features:

  • short name "nan"
  • handy conversion functions
  • select which marshalers you want and limit dependencies to only those you actually need
  • ability to convert your custom structs to nan compatible type with Valid field and all requested encoders/decoders

Supported types:

  • bool
  • float32
  • float64
  • int
  • int8
  • int16
  • int32
  • int64
  • string
  • time.Time
  • uint
  • uint8
  • uint16
  • uint32
  • uint64
  • more types will be added as necessary

Supported marshallers:

  • Standart JSON
  • encoding.TextMarshaler/TextUnmarshaler. Reuses standard JSON logic and format
  • jsoniter
  • easyjson
  • go-json
  • Scylla and Cassandra. Compatible with gocql
  • SQL

Usage

Simply create struct field or variable with one of the exported types and use it without any changes to external API.

JSON input/output will be converted to null or non null values. Scylla and Cassandra will use wire format compatible with gocql.

var data struct {
  Code nan.NullString `json:"code"`
}

b, err := jsoniter.Marshal(data)
if err != nil {
  panic(err)
}

// {"code":null}
println(string(b))

data.Code = nan.String("1")
// Equals to
// data.Code = nan.NullString{String: "1", Valid: true}

b, err = jsoniter.Marshal(data)
if err != nil {
  panic(err)
}

// {"code":"1"}
println(string(b))

code := "2"

// From addr. Can has value or be nil
data.Code = nan.StringAddr(&code)

b, err = jsoniter.Marshal(data)
if err != nil {
  panic(err)
}

// {"code":"2"}
println(string(b))

// To usual value from nan
codeVal := data.Code.String

// 2
println(codeVal)

// To value addr from nan
codeAddr := data.Code.Addr()

// 2
println(*codeAddr)

Helpers

How often you write something like this?

val := true
addr := &val

With nan helpers you can write shorter

addr := nan.Bool(true).Addr()

With this you got less lines of code without readability loose.

Generate marshalers

# go install github.com/kak-tus/nan/cmd/nan@latest
# nan -help

Instead of depending on the whole github.com/kak-tus/nan you can also use nan command to select which marshalers you want. Simply run nan with one or more arguments and it will generate implementations for the specified marshalers in the current directory. For example, running

# nan gen -json -jsoniter

will generate nan.go, json.go, jsoniter.go files in the current working directory that contain only encoding/json and jsoniter marshalers. Nothing else will be generated so you don't have to depend on all the marshalers that github.com/kak-tus/nan supports. Generated files will use current directory name as its package name. You can also specify your own package name with -pkg argument.

Custom structs generator

Imagine, that you have custom struct

type MyStruct struct {
	ID   int
	Name string
}

Use nan command on its file

# nan extra -json -jsoniter example/structs.go

This will generate *_nan.go near source files with json (or any other supported marshallers). And now you have nan compatible struct with all needed marshallers

var val MyStruct

nullVal := NanMyStruct(val)
// Equals to
// nullVal := NullMyStruct{MyStruct: val, Valid: true}

fmt.Println(nullVal.ID)

See example to specific of easyjson, cql, sql generation.

nan extra coommand supports any number of file names at command line.

Benchmarks

See here.

sqlc integration

See here.

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory