An open API service indexing awesome lists of open source software.

https://github.com/datumbrain/nulltypes

GORM null-types JSON marshalling and unmarshalling mixins.
https://github.com/datumbrain/nulltypes

hacktoberfest

Last synced: about 1 year ago
JSON representation

GORM null-types JSON marshalling and unmarshalling mixins.

Awesome Lists containing this project

README

          

# nulltypes [![Build Status](https://api.travis-ci.com/datumbrain/nulltypes.svg?branch=master)](https://travis-ci.com/github/datumbrain/nulltypes) [![Go Report Card](https://goreportcard.com/badge/github.com/datumbrain/nulltypes)](https://goreportcard.com/report/github.com/datumbrain/nulltypes) ![License](https://img.shields.io/badge/license-MIT-blue.svg)

`nulltypes` is a golang module that provides an alternative to nullable data types from `database/sql` with proper JSON marshalling and unmarshalling.

It also provides a wrapper for `time.Time` to format time to use with `timestamp` of SQL databases, i.e. `mysql`, `postgres`.

The default database time zone is set to `UTC`, but it can easily be changed with:

```go
nulltypes.DatabaseLocation, _ = time.LoadLocation([YOUR_TIME_ZONE])
```

## Import

```go
import "github.com/datumbrain/nulltypes"
```

## Usage

Here is an example usage with _GORM_.

```go
package models

type User struct {
ID uint `json:"id" gorm:"primary_key"`
Name string `json:"name"`
Address nulltypes.NullString `json:"address,omitempty"`
CreationDate time.Time `json:"-" gorm:"autoCreateTime:milli;default:current_timestamp"`
UpdationDate nulltypes.NullTime `json:"-" gorm:"autoUpdateTime:milli"`
TerminationDate nulltypes.NullTime `json:"termination_date,omitempty"`
ManagerID nulltypes.NullInt64 `json:"manager_id,omitempty" gorm:"OnUpdate:CASCADE,OnDelete:SET NULL"`
}
```

```go
user := User{
ID: 0,
Name: "John Doe",
Address: nulltypes.String("221B Baker Street"),
CreationDate: time.Now(),
UpdationDate: nulltypes.Now(),
ManagerID: nulltypes.Int64(5),
}
```

## Author

- [Faizan Khalid](https://github.com/iamfaizankhalid)
- [Fahad Siddiqui](https://github.com/fahadsiddiqui)