• gorm 自定义时间、字符串数组类型


    GORM 是GO语言中一款强大友好的ORM框架,但在使用过程中内置的数据类型不能满足以下两个需求,如下:

    1. time.Time类型返回的是 2023-10-03T09:12:08.53528+08:00这种字符串格式,需要额外处理,我们更希望默认的是是2023-10-03 09:12:08这种可读性更高的格式
    2. 有些数据字段需要存储数组形式,如下Article Tags字段希望保存不确定个字符串。直接保存会提示[error] unsupported data type: &[]

    官方提供了 ScannerValuer两个接口,来满足自定义数据的存储、提取,本文记录上述两种结构解决方法。

    type Article struct {
    	Tags  []string
    }
    
    • 1
    • 2
    • 3

    自定义时间类型

    自定义时间类型需满足以下两个需求:

    • 返回2006-01-02 15:04:05格式
    • CreatedAtUpdatedAt使用时能按GORM规范自动填充当前时间

    默认的time.Time是能被gorm自动存储,取出到结构体的,返回2023-10-03T09:12:08.53528+08:00格式原因是在于json序列化时,这里解决方案是自定义一个数据结构,添加JSON Marshal接口,但是自定义的数据类型gorm不能识别,所以要额外添加gorm ScannerValuer两个接口

    type CustomTime time.Time
    
    // GORM Scanner 接口, 从数据库读取到类型
    func (t *CustomTime) Scan(value any) error {
    
    	if v, ok := value.(time.Time); !ok {
    		return errors.Errorf("failed to unmarshal CustomTime value: %v", value)
    	} else {
    		*t = CustomTime(v)
    		return nil
    	}
    }
    
    // GORM Valuer 接口, 保存到数据库
    func (t CustomTime) Value() (driver.Value, error) {
    	if time.Time(t).IsZero() {
    		return nil, nil
    	}
    	return time.Time(t), nil
    }
    
    // JSON Marshal接口,CustomTime结构体转换为json字符串
    func (t *CustomTime) MarshalJSON() ([]byte, error) {
    	t2 := time.Time(*t)
    	return []byte(fmt.Sprintf(`"%v"`, t2.Format("2006-01-02 15:04:05"))), nil
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    自定义字符串数组

    代码比较简单,直接定义一个类型实现 ScannerValuer两个接口,使用中将列定义为Strings类型即可

    type Strings []string
    
    func (s *Strings) Scan(value any) error {
    	v, _ := value.(string)
    	return json.Unmarshal([]byte(v), s)
    }
    func (s Strings) Value() (driver.Value, error) {
    	b, err := json.Marshal(s)
    	return string(b), err
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    测试与完整代码

    测试代码

    package main
    
    import (
    	"database/sql/driver"
    	"encoding/json"
    	"fmt"
    	"time"
    
    	"github.com/pkg/errors"
    	"github.com/glebarez/sqlite"
    	"gorm.io/gorm"
    )
    
    type Strings []string
    
    func (s *Strings) Scan(value any) error {
    	v, _ := value.(string)
    	return json.Unmarshal([]byte(v), s)
    }
    func (s Strings) Value() (driver.Value, error) {
    	b, err := json.Marshal(s)
    	return string(b), err
    }
    
    type CustomTime time.Time
    
    // GORM Scanner 接口, 从数据库读取到类型
    func (t *CustomTime) Scan(value any) error {
    
    	if v, ok := value.(time.Time); !ok {
    		return errors.Errorf("failed to unmarshal CustomTime value: %v", value)
    	} else {
    		*t = CustomTime(v)
    		return nil
    	}
    }
    
    // GORM Valuer 接口, 保存到数据库
    func (t CustomTime) Value() (driver.Value, error) {
    	if time.Time(t).IsZero() {
    		return nil, nil
    	}
    	return time.Time(t), nil
    }
    
    // JSON Marshal接口,CustomTime结构体转换为json字符串
    func (t *CustomTime) MarshalJSON() ([]byte, error) {
    	t2 := time.Time(*t)
    	return []byte(fmt.Sprintf(`"%v"`, t2.Format("2006-01-02 15:04:05"))), nil
    }
    
    // fmt.Printf, 【可选方法】
    func (t CustomTime) String() string {
    	return time.Time(t).Format("2006-01-02 15:04:05")
    }
    
    type Article struct {
    	ID        uint `gorm:"primaryKey"`
    	Tags      Strings
    	CreatedAt CustomTime
    	UpdatedAt CustomTime
    }
    
    func main() {
    	db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
    	if err != nil {
    		panic("failed to connect database")
    	}
    
    	db.AutoMigrate(&Article{})
    	db.Create(&Article{Tags: []string{"go", "java"}})
    
    	var article Article
    
    	db.Last(&article)
    	fmt.Printf("article is: %v\n", article)
    	b, _ := json.Marshal(&article)
    	fmt.Printf("article json is: %s\n", string(b))
    
    	time.Sleep(time.Second * 30)
    	article.Tags = append(article.Tags, "python")
    	db.Save(&article)
    
    	db.Last(&article)
    	fmt.Printf("updated article is: %v\n", article)
    	b, _ = json.Marshal(&article)
    	fmt.Printf("updated article json is: %s\n", string(b))
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88

    测试结果

    字符串数组

    在这里插入图片描述

    自定义时间,可以看到满足2006-01-02 15:04:05格式输出,以及时间自动添加和更新
    在这里插入图片描述

  • 相关阅读:
    2. Mybatis流程
    FFmpeg安装教程
    3万字《SpringBoot微服务开发——Shiro(安全)》
    如何在 Ubuntu 上部署 ONLYOFFICE 协作空间社区版?
    c++(27)STL:容器、算法、迭代器
    智能AI写作系统+ChatGPT程序源码搭建部署教程+支持GPT4.0/AI绘画
    CVE-2021-42287&CVE-2021-42278 域内提权
    数据仓库原理(二)
    github 开源whisper ros llm
    非零基础自学Java (老师:韩顺平) 第8章 面向对象编程(中级部分) 8.11 面向对象编程 - 多态
  • 原文地址:https://blog.csdn.net/LeoForBest/article/details/133606427