dtbam62840 2018-06-08 09:17
浏览 100

如何在Go中为MySQL构建RESTful API?

I have the task of inserting a JSON payload into a table.

The (fixed) SQL table structure is defined like so:

$ echo "describe ut_invitation_api_data\G;" | mysql -h auroradb.dev.unee-t.com -P 3306 -u bugzilla --password=$(aws --profile uneet-dev ssm get-parameters --names MYSQL_PASSWORD --with-decryption --query Parameters[0].Value --output text) bugzilla  | grep Field
mysql: [Warning] Using a password on the command line interface can be insecure.
  Field: id
  Field: mefe_invitation_id
  Field: bzfe_invitor_user_id
  Field: bz_user_id
  Field: user_role_type_id
  Field: is_occupant
  Field: bz_case_id
  Field: bz_unit_id
  Field: invitation_type
  Field: is_mefe_only_user
  Field: user_more
  Field: mefe_invitor_user_id
  Field: processed_datetime
  Field: script
  Field: api_post_datetime

See fields.txt for the details showing different types and whether they can can be null or not.

So my first step is to create a struct and map varchar to string, any type of int to int and dates to time.Time.

Already the column names with underscores is making my Golang editor complain. Next comes the JSON tags and we have something like:

type invitation struct {
    id                   int       `json:"id"`
    mefe_invitation_id   string    `json:"mefe___invitation___id"`
    bzfe_invitor_user_id int       `json:"bzfe___invitor___user___id"`
    bz_user_id           int       `json:"bz___user___id"`
    user_role_type_id    int       `json:"user___role___type___id"`
    is_occupant          bool      `json:"is___occupant"`
    bz_case_id           int       `json:"bz___case___id"`
    bz_unit_id           int       `json:"bz___unit___id"`
    invitation_type      string    `json:"invitation___type"`
    is_mefe_only_user    bool      `json:"is___mefe___only___user"`
    user_more            string    `json:"user___more"`
    mefe_invitor_user_id int       `json:"mefe___invitor___user___id"`
    processed_datetime   time.Time `json:"processed___datetime"`
    script               string    `json:"script"`
    api_post_datetime    time.Time `json:"api___post___datetime"`
}

Which is looking very wrong. Is there an better approach to making RESTful APIs to mysql database using Golang? i.e. ensure a clean mapping and the right RESTful verbs are used?

  • 写回答

1条回答 默认 最新

  • dongshan1396 2018-06-08 11:14
    关注

    The mysql field names must go in the json tag. The field names in your struct can be whatever you like. For example:

    type Invitation struct {
        ID                int    `json:"id"`
        InvitationID      string `json:"mefe_invitation_id"`
        InvitorUserID     int    `json:"bzfe_invitor_user_id"`
        BzUserID          int    `json:"bz_user_id"`
        //...
    }
    

    Also note that in order for json package to be able to see your struct fields they need to be exported (start with capital letters) and you probably want to export your entire struct as well, so you can return it in public functions.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大