登录
首页 >  Golang >  Go问答

利用 jsonb_agg 和 jsonb_build_object 解析嵌套结构

来源:stackoverflow

时间:2024-02-28 12:27:24 218浏览 收藏

哈喽!今天心血来潮给大家带来了《利用 jsonb_agg 和 jsonb_build_object 解析嵌套结构》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

每当我尝试获取(选择/扫描)组(外部结构)及其协作者(内部结构)时,我都会收到以下错误: // sql: 列索引扫描错误...,名称“合作者”: 不支持扫描,将 driver.value 类型 []uint8 存储到类型 *[]user

我正在使用 sqlx(带有 pgx 驱动程序)。

从数据库获取的代码是:

func (psql *postgres) getgroups(someparam string) ([]group, error) {
   groups := []group{}
   err := psql.db.unsafe().select(&groups, , someparam)
   ....
}

type postgres struct {
    db      *sqlx.db
    config  *config.postgresdb
    timeout time.duration
}

这是 sql 查询:

select groups.id, 
       groups.title,
       jsonb_agg(jsonb_build_object(
        'id', u.id,
        'first_name', u.first_name, 
        'last_name', u.last_name,
        'user_pic_url', u.user_pic_url)) as collaborators
from groups
join user_group_permissions p
on   p.group_id = groups.id
join users u
on   u.id = p.user_id

这些是结构:

type group struct {
    id             string  `json:"id" db:"id"`
    title          string  `json:"title"   db:"title"`
    collaborators  []user  `json:"collaborators" db:"collaborators"`
}

type user struct {
    id            string  `json:"id" db:"id"`
    firstname     string  `json:"first_name" db:"first_name"`
    lastname      string  `json:"last_name" db:"last_name"`
    profilephoto  *string `json:"profile_photo" db:"user_pic_url"`
}

我有一个简单的组表、一个用户表和代表具有该组权限的所有用户的表:

create table groups (
   id    int unique not null generated always as identity,
   title text
)
create table users (
    id       bigint unique not null generated always as identity,
    first_name   text not null,
    last_name    text not null,
    user_pic_url text
)
create table user_group_permissions (
   group_id   unsigned_int,
   user_id    unsigned_bigint,
   permission unsigned_smallint,
)
CREATE DOMAIN unsigned_smallint AS smallint
   CHECK(VALUE >= 0 AND VALUE < 32767);

CREATE DOMAIN unsigned_int AS int
   CHECK(VALUE >= 0 AND VALUE < 2147483647);

CREATE DOMAIN unsigned_bigint AS bigint
   CHECK(VALUE >= 0 AND VALUE < 9223372036854775807);

解决方案


import "encoding/json"

type Group struct {
    Id             string   `json:"id" db:"id"`
    Title          string   `json:"title"   db:"title"`
    Collaborators  UserList `json:"collaborators" db:"collaborators"`
}

type User struct {
    Id            string  `json:"id" db:"id"`
    FirstName     string  `json:"first_name" db:"first_name"`
    LastName      string  `json:"last_name" db:"last_name"`
    ProfilePhoto  *string `json:"profile_photo" db:"user_pic_url"`
}

type UserList []User

func (list *UserList) Scan(src interface{}) error {
    var data []byte
    switch v := src.(type) {
    case []byte:
        data = v
    case string:
        data = []byte(v)
    default:
        return nil // or return some error
    }
    return json.Unmarshal(data, list)
}

到这里,我们也就讲完了《利用 jsonb_agg 和 jsonb_build_object 解析嵌套结构》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>