types

package module
v1.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2019 License: MIT Imports: 5 Imported by: 0

README

types-go

Go custom data type

Installation

$ go get github.com/garavan/types-go

Usage

Import types-go to your go project

import types "github.com/garavan/types-go"
Date and Null Date
type User struct {
	Name        string   `json:"name"`
	Dob         types.Date     `json:"dob"`
	DeletedDate types.NullDate `json:"deleted_date"`
}

Marshalling json

dobTime, _ := time.Parse("2006-01-02", "1990-06-03")
eko := User{
	Name:        "Eko Syamsudin",
	Dob:         types.Date{Time: dobTime},
	DeletedDate: types.NullDate{Time: time.Now(), Valid: true},
}

bytes, _ := json.Marshal(&eko)

// json:
// {"name":"Eko Syamsudin","dob":"1990-06-03" "deleted_date":"2019-08-19"}

if deleted date is null

dobTime, _ := time.Parse("2006-01-02", "1990-06-03")
eko := User{
	Name:        "Eko Syamsudin",
	Dob:         types.Date{Time: dobTime},
	DeletedDate: types.NullDate{},
}

bytes, _ := json.Marshal(&eko)

// json:
// {"name":"Eko Syamsudin","dob":"1990-06-03" "deleted_date":"null"}

Unmarshalling json

dani := User{}
err := json.Unmarshal([]byte("{\"name\":\"Ramadani\",\"dob\":\"1992-07-25\"}"), &dani)

// dani.DeletedDate is equal NullDate{}
Gender
type User struct {
	Name   string `json:"name"`
	Gender types.Gender `json:"gender"`
}
eko := User{
	Name:   "Eko Syamsudin",
	Gender: types.Male,
}

bytes, _ := json.Marshal(&eko)
log.Println(string(bytes))

// json:
// {"name":"Eko Syamsudin","gender":"M"}
Null Gender
type User struct {
	Name   string `json:"name"`
	Gender types.NullGender `json:"gender"`
}
eko := User{
	Name:   "Eko Syamsudin",
	Gender: types.NullGender{},
}

bytes, _ := json.Marshal(&eko)
log.Println(string(bytes))

// json:
// {"name":"Eko Syamsudin","gender":"null"}

Tests

go test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Date

type Date struct {
	Time time.Time
}

Date type

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

MarshalJSON date type

func (*Date) Scan

func (d *Date) Scan(value interface{}) error

Scan date scanner

func (*Date) String

func (d *Date) String() string

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(data []byte) error

UnmarshalJSON date type

func (Date) Value

func (d Date) Value() (driver.Value, error)

Value date driver value

type Gender added in v1.0.2

type Gender uint

Gender user gender

const (
	// Unknown gender
	Unknown Gender = iota
	// Male gender
	Male
	// Female gender
	Female
)

func (*Gender) MarshalJSON added in v1.0.2

func (e *Gender) MarshalJSON() ([]byte, error)

MarshalJSON marshalling gender

func (*Gender) Scan added in v1.0.2

func (e *Gender) Scan(src interface{}) error

Scan gender scanner

func (Gender) String added in v1.0.2

func (e Gender) String() string

func (*Gender) UnmarshalJSON added in v1.0.2

func (e *Gender) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshalling gender

func (Gender) Value added in v1.0.2

func (e Gender) Value() (driver.Value, error)

Value gender valuer

type NullDate

type NullDate struct {
	Time  time.Time
	Valid bool
}

NullDate type

func (NullDate) MarshalJSON

func (d NullDate) MarshalJSON() ([]byte, error)

MarshalJSON null date type

func (*NullDate) Scan

func (d *NullDate) Scan(value interface{}) error

Scan null date scanner

func (*NullDate) String

func (d *NullDate) String() string

func (*NullDate) UnmarshalJSON

func (d *NullDate) UnmarshalJSON(data []byte) error

UnmarshalJSON date type

func (NullDate) Value

func (d NullDate) Value() (driver.Value, error)

Value null date driver value

type NullGender added in v1.0.2

type NullGender struct {
	Gender Gender
	Valid  bool
}

NullGender nullable gender

func (NullGender) MarshalJSON added in v1.0.2

func (e NullGender) MarshalJSON() ([]byte, error)

MarshalJSON marshalling null gender

func (*NullGender) Scan added in v1.0.2

func (e *NullGender) Scan(value interface{}) error

Scan null gender scanner

func (*NullGender) UnmarshalJSON added in v1.0.2

func (e *NullGender) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshalling gender

func (NullGender) Value added in v1.0.2

func (e NullGender) Value() (driver.Value, error)

Value null gender value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL