https://github.com/smgladkovskiy/structs
Golang structs - nullables and with support of custom format
https://github.com/smgladkovskiy/structs
bullbool go golang nullable-type nullint64 nullstring nulltime structs
Last synced: about 1 month ago
JSON representation
Golang structs - nullables and with support of custom format
- Host: GitHub
- URL: https://github.com/smgladkovskiy/structs
- Owner: smgladkovskiy
- License: gpl-3.0
- Archived: true
- Created: 2018-10-27T20:22:15.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-08-02T05:44:12.000Z (over 4 years ago)
- Last Synced: 2025-08-14T01:07:09.419Z (7 months ago)
- Topics: bullbool, go, golang, nullable-type, nullint64, nullstring, nulltime, structs
- Language: Go
- Size: 108 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Deprecated
----------
This repo was deprecated. I was young, inexperienced, and I was thinking about the wrong thing. I'm sorry. :)
structs
-------
Various helpful golang structs package
## Zeroable types
Default value is zero value.
* date
* time
## Nullable types
Default value is null value.
* string
* bool
* int64
* float64
* time
* date
## Usage
Get package via `go get github.com/smgladkovskiy/structs` or via `dep ensure add github.com/smgladkovskiy/structs`
if you use dep.
Import package in your go file:
```go
package main
import (
"github.com/smgladkovskiy/structs" // if need to set date or time format
"github.com/smgladkovskiy/structs/null" // if nullables are used
"github.com/smgladkovskiy/structs/zero" // if zeroables are used
)
```
Use in code:
```go
package main
import (
"fmt"
"time"
"github.com/smgladkovskiy/structs" // if need to set date or time format
"github.com/smgladkovskiy/structs/null" // if nullables are used
"github.com/smgladkovskiy/structs/zero" // if zeroables are used
)
func main() {
structs.TimeFormat = func() string {
return time.RFC1123
}
customTime, _ := null.NewTime(time.Now())
if !customTime.Valid {
fmt.Println("time is null")
return
}
fmt.Printf("custom time is %s", customTime.Time)
}
```
## Helper functions and variables
### zero.NewXXX(v interface{}) *zero.XXX
Initiates new zero value of XXX type, using `Scan` method with passed value.
### null.NewXXX(v interface{}) *null.XXX
Initiates new nullable value of XXX type, using `Scan` method with passed value.
### null.NewXXXf(format string, a ...interface{}) *null.XXX
Initiates new `null.String` type value for passed format string and format variables. Available for strings.
### TimeFormat
There is an opportunity for `zero.Time` and `null.Time` types to set time format witch will be used with (Un)MarshallJSON methods.
To override default package format for time (`time.RFC3339`), there must be an `stucts.TimeFormat` function
overriding in your app at the configuration or init level:
```go
package main
import (
"time"
"github.com/smgladkovskiy/structs"
)
func main() {
structs.TimeFormat = func() string {
return time.RFC1123
}
}
```
### DateFormat
For `zero.Date` and `null.Date` types there is the same thing with format for (Un)MarshallJSON.
Default package date format (`YYYY-MM-DD`) must be overridden with `stucts.DateFormat` function:
```go
package main
import (
"github.com/smgladkovskiy/structs"
)
func init() {
structs.DateFormat = func() string {
return "02.01.2006"
}
}
```