Skip to content

Commit

Permalink
feat : adding ulid & documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sutantodadangfit committed Jul 24, 2023
1 parent 891220f commit b3c0da4
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 2 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,47 @@

[![Go Reference](https://pkg.go.dev/badge/github.com/sutantodadang/nullish.svg)](https://pkg.go.dev/github.com/sutantodadang/nullish)

[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://choosealicense.com/licenses/mit/)

Nullish is type helper fo handle null in golang. its support null for primitive data types and also another data type.

## Warning ⚠️

This project is used by my companies on production and there is not enough time to make unit testing so maybe something not work for you. use at your own risk.

## Installation

```bash
go get -u github.com/sutantodadang/nullish
```

## Features

- NullString
- NullJsonb / Json
- NullInt
- NullFloat
- NullBool
- NullUUID
- NullTime
- NullULID

## Usage/Examples

- nullstring

```go
import "github.com/sutantodadang/nullish"

type foo struct {
bar nullish.NullString

}

// you can also defined default value
foo.bar = nullish.NewNullString("hello", true)
```

## Authors

- [@sutantodadang](https://www.github.com/sutantodadang)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require github.com/goccy/go-json v0.10.2
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/oklog/ulid/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.2 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
4 changes: 2 additions & 2 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (nt NullTime) MarshalJSON() ([]byte, error) {
return NullType, nil
}

return json.Marshal(nt.Time.Format(time.RFC3339))
return json.Marshal(nt.Time.Format(time.RFC3339Nano))
}

// UnmarshalJSON method
Expand All @@ -66,7 +66,7 @@ func (nt *NullTime) UnmarshalJSON(data []byte) error {
return err
}

pTime, err := time.Parse(time.RFC3339, res)
pTime, err := time.Parse(time.RFC3339Nano, res)
if err != nil {
return err
}
Expand Down
72 changes: 72 additions & 0 deletions ulid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package nullish

import (
"bytes"
"database/sql/driver"
"errors"

"github.com/goccy/go-json"
"github.com/oklog/ulid/v2"
)

type NullULID struct {
ULID ulid.ULID
Valid bool
}

// Value method
func (nl NullULID) Value() (driver.Value, error) {

if !nl.Valid {
return nil, nil
}

return nl.ULID.Value()
}

// Scan method
func (nl *NullULID) Scan(value interface{}) error {

if value == nil {
nl.ULID, nl.Valid = ulid.ULID{}, false
return nil
}

err := nl.ULID.Scan(value)
if err != nil {
nl.ULID, nl.Valid = ulid.ULID{}, false
return errors.New("scan ulid is failed")

}

nl.Valid = true

return nil
}

// MarshalJSON method
func (nl NullULID) MarshalJSON() ([]byte, error) {

if !nl.Valid {
return NullType, nil
}

return json.Marshal(nl.ULID)
}

// UnmarshalJSON method
func (nl *NullULID) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, NullType) {
*nl = NullULID{}
return nil
}

err := json.Unmarshal(data, &nl.ULID)
if err != nil {
return err
}

nl.Valid = true

return nil
}

0 comments on commit b3c0da4

Please sign in to comment.