Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmarshallers

Overview

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
  • more types will be added as necessary

Supported marshallers:

  • Standart JSON
  • jsoniter
  • easyjson
  • 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}
	fmt.Println(string(b))

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

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

	// {"code":"1"}
	fmt.Println(string(b))

Generate marshalers

# go get github.com/kak-tus/nan/cmd/nan
# 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)
// Or
// 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.

You might also like...
Algorithms and Data Structures Solved in Golang

Algorithms and Data Structures Solved in Golang Hi! I'm Bruno Melo and this repository contains a lot challenges solved on many plataforms using go as

Some data structures and algorithms using golang

Some data structures and algorithms using golang

Data structures and algorithms implementation from ZeroToMastery course

ZeroToMastery Data Structures & Algorithms course This repo includes all the data structure and algorithm exercises solutions and implementations. Ins

Practice-dsa-go - Data Structures and Algorithms for Interview Preparation in Go

Data Structures and Algorithms for Interview Preparation in Go Data Structures K

Implementation of various data structures and algorithms in Go
Implementation of various data structures and algorithms in Go

GoDS (Go Data Structures) Implementation of various data structures and algorithms in Go. Data Structures Containers Lists ArrayList SinglyLinkedList

Tutorial code for my video Learn to Use Basic Data Structures - Slices, Structs and Maps in Golang

Learn to Use Basic Data Structures - Slices, Structs and Maps in Golang Read text from a file and split into words. Introduction to slices / lists. Co

Data Structures and Algorithms implementation in Go

Data Structures and Algorithms Clean and simple implementation in Go Implementation There are several data structures and algorithms implemented in th

dagger is a fast, concurrency safe, mutable, in-memory directed graph library with zero dependencies
dagger is a fast, concurrency safe, mutable, in-memory directed graph library with zero dependencies

dagger is a blazing fast, concurrency safe, mutable, in-memory directed graph implementation with zero dependencies

 A tree like tool help you to explore data structures in your redis server
A tree like tool help you to explore data structures in your redis server

Redis-view is a tree like tool help you explore data structures in your redis server

Owner
Andrey Kuzmin
Andrey Kuzmin
Type-safe, zero-allocation sets for Go

Set Package set is a type-safe, zero-allocation port of the excellent package fatih/set. It contains sets for most of the basic types and you can gene

ScyllaDB 796 Jan 5, 2023
Nullable Go types that can be marshalled/unmarshalled to/from JSON.

Nullable Go types Description This package provides nullable Go types for bool, float64, int64, int32, string and time.Time replacing sql.NullString,

Emvi 30 Dec 12, 2022
Spanner - A handy tool for visualising Datadog traces

Spanner A minimal tool for visualising Datadog traces ?? Installation You can in

Marcus Crane 0 Jan 2, 2022
Package iter provides generic, lazy iterators, functions for producing them from primitive types, as well as functions and methods for transforming and consuming them.

iter Package iter provides generic, lazy iterators, functions for producing them from primitive types, as well as functions and methods for transformi

Matthew Toohey 26 Dec 16, 2022
Package set is a small wrapper around the official reflect package that facilitates loose type conversion and assignment into native Go types.

Package set is a small wrapper around the official reflect package that facilitates loose type conversion and assignment into native Go types. Read th

null 44 Dec 27, 2022
Provides conversion from athena outputs to strongly-typed data models.

Provides conversion from athena outputs to strongly defined data models. Getting started Given the following data struct you define: type MyModel stru

Ken Timothy 4 Feb 7, 2022
Go library for encoding native Go structures into generic map values.

wstructs origin: github.com/things-go/structs Go library for encoding native Go structures into generic map values. Installation Use go get. go ge

null 0 Jan 10, 2022
Library of generic data structures for Go.

gods Library of generic data structures for Go. priority queue sorted list priority queue unsorted list priority queue heap priority queue adaptable h

Mehdi Eidi 16 Dec 26, 2022
Graph algorithms and data structures

Your basic graph Golang library of basic graph algorithms Topological ordering, image by David Eppstein, CC0 1.0. This library offers efficient and we

Algorithms to Go 616 Jan 2, 2023
Graph algorithms and data structures

Your basic graph Golang library of basic graph algorithms Topological ordering, image by David Eppstein, CC0 1.0. This library offers efficient and we

Algorithms to Go 9 Jan 25, 2021