From 82501ff57c7eb3962c1478967278b03b870f6d2f Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 19 Jun 2024 16:07:39 +0800 Subject: [PATCH] fix reflection --- migrations/mysql/20240531163411_trades_created.sql | 2 +- pkg/service/reflect.go | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/migrations/mysql/20240531163411_trades_created.sql b/migrations/mysql/20240531163411_trades_created.sql index 001b267c9..1b2323796 100644 --- a/migrations/mysql/20240531163411_trades_created.sql +++ b/migrations/mysql/20240531163411_trades_created.sql @@ -1,6 +1,6 @@ -- +up -- +begin -ALTER TABLE `trades` ADD COLUMN `inserted_at` DATETIME(3) DEFAULT CURRENT_TIMESTAMP NOT NULL AFTER `traded_at`; +ALTER TABLE `trades` ADD COLUMN `inserted_at` DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL AFTER `traded_at`; -- +end -- +begin diff --git a/pkg/service/reflect.go b/pkg/service/reflect.go index bcf76b08b..c2acc65aa 100644 --- a/pkg/service/reflect.go +++ b/pkg/service/reflect.go @@ -27,13 +27,16 @@ func tableNameOf(record interface{}) string { } func placeholdersOf(record interface{}) []string { - vt := reflect.ValueOf(record) rt := reflect.TypeOf(record) - if rt.Kind() == reflect.Ptr { rt = rt.Elem() } + vt := reflect.ValueOf(record) + if vt.Kind() == reflect.Ptr { + vt = vt.Elem() + } + if rt.Kind() != reflect.Struct { return nil } @@ -60,13 +63,16 @@ func placeholdersOf(record interface{}) []string { } func fieldsNamesOf(record interface{}) []string { - vt := reflect.ValueOf(record) rt := reflect.TypeOf(record) - if rt.Kind() == reflect.Ptr { rt = rt.Elem() } + vt := reflect.ValueOf(record) + if vt.Kind() == reflect.Ptr { + vt = vt.Elem() + } + if rt.Kind() != reflect.Struct { return nil }