Read Map inside Interface

Hi,

I have used a package to unserialize php serialized data and it gives me interface having maps inside. How I can read the maps inside that interface?

Package is: github.com/wulijun/go-php-serialize/phpserialize

Once I get unserialized data the result is as follow:

map[type:feedexport/rule_condition_combine attribute:<nil> operator:<nil> value:1 
is_value_processed:<nil> aggregator:any 
conditions:map[0:map[type:feedexport/rule_condition_product attribute:sku operator:== 
value:AMW-1900-50SLE-ROOM is_value_processed:false] 1:map[attribute:sku operator:== 
value:ASL-B654-77-74-98-ROOM is_value_processed:false 
type:feedexport/rule_condition_product] 2:map[value:ASL-B217-57-54S-95-ROOM 
is_value_processed:false type:feedexport/rule_condition_product attribute:sku operator:==]]]

Can any one help me, please.

You need to do a type assertion or a type switch to ensure the correct type, then you can process it as usual.

PS: AFAIR it will be a map[interface{}]interface{}, where you need to cast and assert keys and values again when accessing the map. When I had to work with that library code became very ugly because of this. It will be probably much easier if you can convince the source to hand out JSON instead :wink:

 package main

 import (
     "fmt"
  "github.com/wulijun/go-php-serialize/phpserialize"
 )

func main() {
rules := RulesList()

for key := range rules {
	fmt.Println(rules[key])
}

}

type Rule struct {
RuleId 	   int 		   	 `json:"rule_id"`
Conditions interface{} `json:"conditions"`
}

func RulesList() ([]Rule) {
db := DbConn()
res, err := db.Query(`SELECT r.rule_id, r.conditions_serialized AS conditions FROM 
    m_feedexport_rule AS r`)
CheckError(err)

rule  := Rule{}
rules := []Rule{}
for res.Next()  {
	var ruleId int
	var conditions string
	err = res.Scan(&ruleId, &conditions)
	CheckError(err)

	conditionsInterface := make(map[interface{}]interface{})
	var condition []interface{}

	cons, err := phpserialize.Decode(conditions)
	CheckError(err)
	conditionsInterface[ruleId] = cons

	condition = append(condition, conditionsInterface)

	rule.RuleId     = ruleId
	rule.Conditions = condition

	rules = append(rules, rule)
}

return rules
}

This is the code I want to execute.

Is this snippet of code meant as a follow up question to my proposed solution?

If so, sorry, I do not understand what you want to ask by that, if not, I still do not understand.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.