我的任务是将JSON有效负载插入表中 .
(固定)SQL表结构的定义如下:
$ 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
有关显示不同类型的详细信息以及它们是否可以为null,请参阅fields.txt .
所以我的第一步是创建一个struct并将varchar映射到string,将任何类型的int映射到int,将date映射到time.Time .
带有下划线的列名已经让我的Golang编辑器抱怨了 . 接下来是JSON标签,我们有类似的东西:
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"`
}
这看起来非常错误 . 有没有更好的方法使用Golang将RESTful API创建到mysql数据库?即确保使用干净的映射并使用正确的RESTful动词?