Skip to content

Commit

Permalink
database_update_tool: add function to extract sql tag
Browse files Browse the repository at this point in the history
  • Loading branch information
harissudrajat authored and agungdwiprasetyo committed Jun 19, 2024
1 parent f9dba3e commit aeccc2e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
9 changes: 9 additions & 0 deletions candishared/database_update_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ func DBUpdateMongoExtractorKey(structField reflect.StructField) (string, bool) {
return candihelper.ToDelimited(structField.Name, '_'), false
}

// DBUpdateSqlExtractorKey struct field key extractor for mongo model
func DBUpdateSqlExtractorKey(structField reflect.StructField) (string, bool) {
sqlTag := structField.Tag.Get("sql")
if strings.HasPrefix(sqlTag, "column:") {
return strings.Split(strings.TrimPrefix(sqlTag, "column:"), ";")[0], false
}
return candihelper.ToDelimited(structField.Name, '_'), false
}

// DBUpdateTools for construct selected field to update
type DBUpdateTools struct {
KeyExtractorFunc func(structTag reflect.StructField) (key string, mustSet bool)
Expand Down
67 changes: 67 additions & 0 deletions candishared/database_update_tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,70 @@ func TestDBUpdateToolsMongo(t *testing.T) {
)
assert.Equal(t, 9, len(updated))
}

func TestDBUpdateSqlExtractorKey(t *testing.T) {
type SubModel struct {
Title string `sql:"column:title" json:"title"`
Profile string `sql:"column:profile" json:"profile"`
ActivatedAt sql.NullTime `sql:"column:activated_at" json:"activatedAt"`
CityAddress string `sql:"type:text"`
}
type Model struct {
ID int `sql:"column:id;" json:"id"`
Name *string `sql:"column:name;" json:"name"`
Address string `sql:"column:alamat" json:"address"`
No int
CreatedAt time.Time
IgnoreMe SubModel `sql:"column:test" json:"ignoreMe" ignoreUpdate:"true"`
Rel SubModel `sql:"foreignKey:ID" json:"rel"`
Log []byte
StrArr pq.StringArray
IntArr pq.Int64Array
Ch chan string
Multi []SubModel
Map map[int]SubModel
PtrModel *SubModel
DeletedAt *time.Time
NamedArg *sql.NamedArg
SubModel
}
var updated map[string]any

updated = DBUpdateTools{KeyExtractorFunc: DBUpdateSqlExtractorKey}.ToMap(
&Model{ID: 1, Name: candihelper.ToStringPtr("01"), Address: "street",
SubModel: SubModel{Title: "test", CityAddress: "Jakarta"}, Rel: SubModel{Title: "rel sub"}},
DBUpdateSetUpdatedFields("ID", "Name", "Title", "CityAddress", "Address"),
)

assert.Equal(t, 5, len(updated))
assert.Equal(t, 1, updated["id"])
assert.Equal(t, "01", updated["name"])
assert.Equal(t, "test", updated["title"])
assert.Equal(t, "Jakarta", updated["city_address"])

updated = DBUpdateTools{KeyExtractorFunc: DBUpdateSqlExtractorKey}.ToMap(
Model{ID: 1, Name: candihelper.ToStringPtr("01"), Address: "street", SubModel: SubModel{Title: "test", ActivatedAt: sql.NullTime{Valid: true}}},
DBUpdateSetIgnoredFields("ID", "Name", "Title"),
)
assert.Equal(t, 10, len(updated))
assert.Equal(t, "street", updated["alamat"])

updated = DBUpdateTools{KeyExtractorFunc: DBUpdateSqlExtractorKey}.ToMap(
Model{
No: 10,
Multi: make([]SubModel, 1),
Rel: SubModel{Title: "001"},
CreatedAt: time.Now(),
SubModel: SubModel{ActivatedAt: sql.NullTime{Valid: true, Time: time.Now()}, CityAddress: "Jakarta"},
PtrModel: &SubModel{CityAddress: "New"},
Log: []byte(`123`),
StrArr: pq.StringArray{"1", "2", "3"},
IntArr: pq.Int64Array{1, 2, 3},
},
)
assert.Equal(t, 13, len(updated))
assert.Equal(t, "Jakarta", updated["city_address"])
assert.Equal(t, 10, updated["no"])
assert.Equal(t, "{\"1\",\"2\",\"3\"}", updated["str_arr"])
assert.Equal(t, []byte(`123`), updated["log"])
}

0 comments on commit aeccc2e

Please sign in to comment.