dateparse_tag

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

README

dateparse_tag

dateparse time by struct tag

intro&简介

WithTagName() // 自定义你想要使用的tag名称,默认为dateFormat
WithDefaultTagValue() // 定义这个tag的默认值,默认为 default
WithDefaultFormat() // 定义时间格式化样式,默认为 2006-01-02 15:04:05
WithEmptyValue() // 定义一个空值返回,当指定结构体的指定字段为空值时,返回你想要的空值,默认为 ""

install&安装

    go get github.com/cowardmrx/dateparse_tag

use&使用

type User struct {
    Name     string `json:"name"`
    BirthDay string `json:"birth_day" format_date:"default"`
}

func TestNewDateParseTag(t *testing.T) {
    u := new(User)
    
    u.Name = "张三"
    u.BirthDay = time.Now().String()
    
    t.Logf("user : %v", u)
    
    dp := NewDateParseTag(WithTagName("format_date"))
    
    dp.Parse(u, u)
    
    t.Logf("user new %v", u)
}

// old user : &{张三 2022-01-12 14:10:17.1867047 +0800 CST m=+0.003444301}
// new user  &{张三 2022-01-12 14:10:17}

Documentation

Index

Constants

View Source
const (
	DefaultFormatLayout = "2006-01-02 15:04:05"
	DefaultTagName      = "dateFormat"
	DefaultTagValue     = "default"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DateParseTag

type DateParseTag interface {
	Parse(in, out interface{})
}

func NewDateParseTag

func NewDateParseTag(opts ...Options) DateParseTag

type Options

type Options func(dp *dateParseTag)

func WithDefaultFormat

func WithDefaultFormat(defaultFormatLayout string) Options

func WithDefaultTagValue

func WithDefaultTagValue(tagVal string) Options

func WithEmptyValue

func WithEmptyValue(emptyValue string) Options

func WithTagName

func WithTagName(tagName string) Options

Jump to

Keyboard shortcuts

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