将Postgresql jsonb查询响应字符串解组为golang嵌套结构

时间:2019-06-12 09:24:46

标签: postgresql go

我正在尝试从Postgresql中读取jsonb和非jsonb列中的一些数据,然后解组我要到达的嵌套结构的响应字符串。

我能够以字符串形式检索tha数据。但是我不知道如何将其解组到结构中。

package main
import (
    "database/sql"
    "fmt"
    "log"
    "strconv"

    _ "github.com/lib/pq"
)

type Token struct {
    Name     string
    Value    string
    Path     string
    HttpOnly bool
}

type Session struct {
    Phishlet   string                       `json:"phishlet,omitempty"`
    LandingURL string                       `json:"landing_url,omitempty"`
    Username   string                       `json:"username,omitempty"`
    Password   string                       `json:"password,omitempty"`
    Custom     map[string]string            `json:"custom,omitempty"`
    Tokens     map[string]map[string]*Token `json:"tokens,omitempty"`
    SessionId  string                       `json:"session_id,omitempty"`
    UserAgent  string                       `json:"useragent,omitempty"`
    RemoteAddr string                       `json:"remote_addr,omitempty"`
    CreateTime int64                        `json:"create_time,omitempty"`
    UpdateTime int64                        `json:"update_time,omitempty"`
}

type Sessions struct {
    Id      int64
    UserId  []byte
    Session Session
}



func main() {

    db, err := sql.Open("postgres", "postgres://user:pass@localhost:port/db")
    if err != nil {
        log.Fatal(err)
    }

    rows, err := db.Query("SELECT id, user_id,session FROM sessions ORDER BY id")
    defer rows.Close()
    columns, err := rows.Columns()
    if err != nil {
        log.Fatal(err)
    }
    count := len(columns)
    values := make([]interface{}, count)
    scanArgs := make([]interface{}, count)
    for i := range values {
        scanArgs[i] = &values[i]
    }

    data := make(map[string][]interface{})

    for rows.Next() {
        err = rows.Scan(scanArgs...)
        if err != nil {
            panic(fmt.Sprintf("rows.Scan: %v", err))
        }
        for i, v := range values {
            switch col := columns[i]; col {
            case "id":
                fmt.Printf("Id: %v\n", v)
            case "user_id":
                fmt.Printf("UserId: %s\n", v)
            case "session":
                x := v.([]byte)
                if nx, ok := strconv.ParseFloat(string(x), 64); ok == nil {
                    data[columns[i]] = append(data[columns[i]], nx)
                } else if b, ok := strconv.ParseBool(string(x)); ok == nil {
                    data[columns[i]] = append(data[columns[i]], b)
                } else if "string" == fmt.Sprintf("%T", string(x)) {
                    data[columns[i]] = append(data[columns[i]], string(x))
                } else {
                    fmt.Printf("Failed on if for type %T of %v\n", x, x)
                }
            }
            fmt.Println(data)
        }
    }
}

//Database schema
create table if not exists sessions(
            id bigserial not null primary key,
            user_id uuid not null references users(id)
            session jsonb
    )

我希望在成功取消编组到struct

之后的输出是这个
[
  {
    "id": 1,
    "user_id": "c610ceb9-640f-43d2-8e26-a80be6afb831",
    "phishlet": "niko",
    "landing_url": "https://niko.com/ke/login",
    "username": "MyUser",
    "password": "my_p@55",
    "custom": {},
    "tokens": {
      ".niko.com": {
        "_niko_sess": {
          "Name": "_niko_sess",
          "Value": "BAh7CiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrCG6lp1hqAToMY3NyZl9p%250AZCIlYjk4MGFiZDEyNGEzZTI0NjE0MzYzZDkyNTRmOWZiYzQ6B2lkIiVhOTA3%250AMjJjOGFkNDdlNzFiZmYyNmEwZjU2ZDBhYTYyYToJdXNlcmwrCQDgV68MtUMP--48e2dcf262fa4895366025210c10ff4c922a3fca",
          "Path": "/",
          "HttpOnly": true
        },
        "auth_token": {
          "Name": "auth_token",
          "Value": "e9b42a3b76907dff7b55117627a3be40e4c557ce",
          "Path": "",
          "HttpOnly": true
        },
        "nkt": {
          "Name": "nkt",
          "Value": "FdRf8fAkS8bEthMBUaY2gNqbTjFdZcUFh8wJBbR4",
          "Path": "",
          "HttpOnly": true
        },
        "nikd": {
          "Name": "nikd",
          "Value": "u=1099921800094539776",
          "Path": "",
          "HttpOnly": false
        }
      }
    },
    "session_id": "cd5c9c35b96cfdaac5e299b21c40a9fbad667e1710c6f1efb972008ac3bac55c",
    "useragent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0",
    "remote_addr": "197.232.6.251",
    "create_time": 1556265484,
    "update_time": 1556265594
  },
  {
    "id": 2,
    "user_id": "d52673ef-cf4e-4081-855f-d7dd91762633",
    "phishlet": "niko",
    "landing_url": "https://niko.com/ke/login",
    "username": "MyUser",
    "password": "my_p@55",
    "custom": {},
    "tokens": {
      ".niko.com": {
        "_niko_sess": {
          "Name": "_niko_sess",
          "Value": "BAh7CiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrCG6lp1hqAToMY3NyZl9p%250AZCIlYjk4MGFiZDEyNGEzZTI0NjE0MzYzZDkyNTRmOWZiYzQ6B2lkIiVhOTA3%250AMjJjOGFkNDdlNzFiZmYyNmEwZjU2ZDBhYTYyYToJdXNlcmwrCQDgV68MtUMP--48e2dcf262fa4895366025210c10ff4c922a3fca",
          "Path": "/",
          "HttpOnly": true
        },
        "auth_token": {
          "Name": "auth_token",
          "Value": "e9b42a3b76907dff7b55117627a3be40e4c557ce",
          "Path": "",
          "HttpOnly": true
        },
        "nkt": {
          "Name": "nkt",
          "Value": "FdRf8fAkS8bEthMBUaY2gNqbTjFdZcUFh8wJBbR4",
          "Path": "",
          "HttpOnly": true
        },
        "nikd": {
          "Name": "nikd",
          "Value": "u=1099921800094539776",
          "Path": "",
          "HttpOnly": false
        }
      }
    },
    "session_id": "cd5c9c35b96cfdaac5e299b21c40a9fbad667e1710c6f1efb972008ac3bac55c",
    "useragent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0",
    "remote_addr": "197.232.6.251",
    "create_time": 1556265484,
    "update_time": 1556265594
  }
]

,而是从数据库中以string类型获取它,如下所示。

map[session:[
  {
    "phishlet": "niko",
    "landing_url": "https://niko.com/ke/login",
    "username": "MyUser",
    "password": "my_p@55",
    "custom": {},
    "tokens": {
      ".niko.com": {
        "_niko_sess": {
          "Name": "_niko_sess",
          "Value": "BAh7CiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrCG6lp1hqAToMY3NyZl9p%250AZCIlYjk4MGFiZDEyNGEzZTI0NjE0MzYzZDkyNTRmOWZiYzQ6B2lkIiVhOTA3%250AMjJjOGFkNDdlNzFiZmYyNmEwZjU2ZDBhYTYyYToJdXNlcmwrCQDgV68MtUMP--48e2dcf262fa4895366025210c10ff4c922a3fca",
          "Path": "/",
          "HttpOnly": true
        },
        "auth_token": {
          "Name": "auth_token",
          "Value": "e9b42a3b76907dff7b55117627a3be40e4c557ce",
          "Path": "",
          "HttpOnly": true
        },
        "nkt": {
          "Name": "nkt",
          "Value": "FdRf8fAkS8bEthMBUaY2gNqbTjFdZcUFh8wJBbR4",
          "Path": "",
          "HttpOnly": true
        },
        "nikd": {
          "Name": "nikd",
          "Value": "u=1099921800094539776",
          "Path": "",
          "HttpOnly": false
        }
      }
    },
    "session_id": "cd5c9c35b96cfdaac5e299b21c40a9fbad667e1710c6f1efb972008ac3bac55c",
    "useragent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0",
    "remote_addr": "197.232.6.251",
    "create_time": 1556265484,
    "update_time": 1556265594
  }]]
  map[session:[{
    "phishlet": "niko",
    "landing_url": "https://niko.com/ke/login",
    "username": "MyUser",
    "password": "my_p@55",
    "custom": {},
    "tokens": {
      ".niko.com": {
        "_niko_sess": {
          "Name": "_niko_sess",
          "Value": "BAh7CiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrCG6lp1hqAToMY3NyZl9p%250AZCIlYjk4MGFiZDEyNGEzZTI0NjE0MzYzZDkyNTRmOWZiYzQ6B2lkIiVhOTA3%250AMjJjOGFkNDdlNzFiZmYyNmEwZjU2ZDBhYTYyYToJdXNlcmwrCQDgV68MtUMP--48e2dcf262fa4895366025210c10ff4c922a3fca",
          "Path": "/",
          "HttpOnly": true
        },
        "auth_token": {
          "Name": "auth_token",
          "Value": "e9b42a3b76907dff7b55117627a3be40e4c557ce",
          "Path": "",
          "HttpOnly": true
        },
        "nkt": {
          "Name": "nkt",
          "Value": "FdRf8fAkS8bEthMBUaY2gNqbTjFdZcUFh8wJBbR4",
          "Path": "",
          "HttpOnly": true
        },
        "nikd": {
          "Name": "nikd",
          "Value": "u=1099921800094539776",
          "Path": "",
          "HttpOnly": false
        }
      }
    },
    "session_id": "cd5c9c35b96cfdaac5e299b21c40a9fbad667e1710c6f1efb972008ac3bac55c",
    "useragent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0",
    "remote_addr": "197.232.6.251",
    "create_time": 1556265484,
    "update_time": 1556265594
  }
]]

1 个答案:

答案 0 :(得分:0)

我认为这是pq驱动程序的预期行为-请参见:https://github.com/lib/pq/issues/348

有一个建议在问题线程中使用sqlx -我尚未尝试过:https://github.com/lib/pq/issues/348#issuecomment-282742349

您还可以从该驱动程序的文档将驱动程序切换为pgx:https://github.com/jackc/pgx

  

pgx包含内置支持,可在Go类型与PostgreSQL JSON和JSONB之间进行封送和封送处理。

我过去曾经使用pgx进行jsonb解组-效果很好。