🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

github.com/liquid-labs/go-nullable-mysql

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/liquid-labs/go-nullable-mysql

v1.0.2
Source
Go
Version published
Created
Source

go-nullable-mysql

Enhanced support of NULL values in JSON marshalling and unmarshalling for MySQL datatypes in Go. I.e., SQL NULL <-> JSON null. Also, add support for MySQL DATE types.

Usage

import (
...
  "database/sql"
  "time"

  "github.com/Liquid-Labs/go-nullable-mysql/nulls"
)

type Foo struct {
  ADate       nulls.Date      `json:"aDate"`
  ATimestamp  nulls.Timestamp `json:"aTime"`
}

func SaveData(foo *Foo) (error) {
  _, err := insertFooStmt.Exec(foo.ADate, foo.ATimestamp)
  ...
}

func ScanFoo(row sql.Row) (*Foo, error) {
  foo := Foo{}
  if err := row.Scan(&foo.ADate, &foo.ATimestamp); err != nil {
    return nil, err
   }
   return &foo, nil
}

func MakeFoo(dateString string, time time.timeStamp) (Foo) {
  return Foo{nulls.NewDate(dateString), nulls.NewTimestamp(time)}
}

Supported types are:

  • NullBool - a MySQL BOOL (==TINYINT(1)) <-> golang bool
  • NullDate - a MySQL DATE <-> golang string in 'YYYY-MM-DD' format
  • NullFloat64 - a MySQL DOUBLE (or FLOAT if you like) <-> golang float64
  • NullInt64 - a MySQL INT <-> golang int64
  • NullString - a MySQL VARCHAR, CHAR, TEXT, etc. <-> golang string
  • NullTimestamp - a MySQL TIMESTAMP <-> golang time.Time

References

  • This work was based on implementation by Supid Ravel as discusesd in this Medium article.
  • guregu/null was used as an exapmle to kickstart the unit tests. guregu/null is a good alternative with similar functionality and a slightly different feature set.

FAQs

Package last updated on 29 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts