Skip to content

Instantly share code, notes, and snippets.

@lengocgiang
Created August 30, 2017 07:31
Show Gist options
  • Save lengocgiang/a661f25bfba523824d502a77bd69e8a0 to your computer and use it in GitHub Desktop.
Save lengocgiang/a661f25bfba523824d502a77bd69e8a0 to your computer and use it in GitHub Desktop.
reflect code
// old
func getPreferredProductRefIdsFromContactProgress(contactProgress models.ContactProgress) []int {
if !contactProgress.IsEmpty() {
enterInfo := contactProgress.EnterInfo
enterInfoMap := make(map[string]interface{})
json.Unmarshal([]byte(enterInfo), &enterInfoMap)
if enterInfoMap["product_ref_ids"] != nil {
value := reflect.ValueOf(enterInfoMap["product_ref_ids"])
fmt.Println(value.Kind(), enterInfoMap["product_ref_ids"])
var rawData string
var productRefId []int
if value.Kind() == reflect.String {
rawData = enterInfoMap["product_ref_ids"].(string)
rawData = strings.Replace(rawData, "\"", "", -1)
json.Unmarshal([]byte(rawData), &productRefId)
} else {
for i := 0; i < value.Len(); i++ {
str := value.Index(i).Interface().(string)
refId, _ := strconv.Atoi(str)
productRefId = append(productRefId, refId)
}
}
return productRefId
}
}
return
}
// new
func getPreferredProductRefIdsFromContactProgress(contactProgress models.ContactProgress) ([]int, error) {
if contactProgress.IsEmpty() {
return nil, nil
}
// generate the map for parsing the initial json document
enterInfo := make(map[string]interface{})
err := json.Unmarshal([]byte(contactProgress.EnterInfo), &enterInfo)
// parse the document, get the error
if err != nil {
return nil, err
}
// get the value of the enterInfo["product_ref_ids"]
// and the 'ok' value to know whatever the key exists
productRefIDs, ok := enterInfo["product_ref_ids"]
if !ok {
return nil, errors.New("no product_ref_ids key found in JSON")
}
return parseProductRefIDs(productRefIDs)
}
func parseProductRefIDs(productRefIDs interface{}) ([]int, error) {
var productRefIDsSlice []int
// detect productRefIDs type
// sometimes has string or slice
v := reflect.ValueOf(productRefIDs)
switch v.Kind() {
case reflect.String:
// remove " symbol if exits
cleanProductRefIDs := strings.Replace(productRefIDs.(string), "\"", "", -1)
// parse the product_ref_ids valus as JSON and capture this error
err := json.Unmarshal([]byte(cleanProductRefIDs), &productRefIDsSlice)
if err != nil {
return nil, err
}
// create an []int with 0 items in it,
// but enought memory allocated to hold the number of elements in productRefIDsSlice
ids := make([]int, 0, len(productRefIDsSlice))
for _, id := range productRefIDsSlice {
ids = append(ids, id)
}
// return the result ids slice
return ids, nil
case reflect.Slice:
// create an []int with 0 items in it.
ids := make([]int, 0, v.Len())
for i := 0; i < v.Len(); i++ {
// get string id item in v
idStr := v.Index(i).Interface().(string)
id, _ := strconv.Atoi(idStr)
ids = append(ids, id)
}
// return ids result ids slice
return ids, nil
default:
return nil, nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment