Skip to content

Commit

Permalink
add method to put all values together
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Tanthowi Jauhari committed Aug 24, 2020
1 parent fd11f40 commit 780d524
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ import (

func main() {
var user models.UserModel
utils.ExtractModels(user)
var exceptField = []string{"fullname"}
utils.ExtractModelField(user, exceptField)
}
32 changes: 30 additions & 2 deletions utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
)

func ExtractModels(model interface{}) {
func ExtractModelField(model interface{}, exceptionalField []string) {
var reflectValue = reflect.ValueOf(model)

if reflectValue.Kind() == reflect.Ptr {
Expand All @@ -15,6 +15,34 @@ func ExtractModels(model interface{}) {
var reflectType = reflectValue.Type()

for i := 0; i < reflectValue.NumField(); i++ {
fmt.Println(reflectType.Field(i).Tag.Get("json"))
for _, item := range exceptionalField {
current_field := reflectType.Field(i).Tag.Get("json")
if current_field != item {
fmt.Println(current_field)
}
}
}
}

func PutAllValueTogether(models [][]interface{}) string {
var values string

for indexCols, cols := range models {
var valueRows string
for indexRows, rows := range cols {
if (indexRows + 1) < len(cols) {
valueRows += fmt.Sprintf("'%v', ", rows.(string))
} else {
valueRows += fmt.Sprintf("'%v' ", rows.(string))
}
}

if (indexCols + 1) < len(cols) {
values += fmt.Sprintf("(%v),", valueRows)
} else {
values += fmt.Sprintf("(%v);", valueRows)
}
}

return values
}

0 comments on commit 780d524

Please sign in to comment.