I have a web app which uses a session struct with a composed model struct.
type Session struct {
SessionModel
}
type SessionModel interface { }
UserSession is the session model composed within Session
type UserSession struct {
Name string
Email string
Password string
}
When I unmarshal JSON session data from Redis, a map is stored in the SessionModel field (the interface which allows classes to embed).
func(r SessionStore) Get(s Session, id string) (Session, error){
bson, err := r.Client.Get(id).Bytes()
if err != nil {
return s, err
}
if err := json.Unmarshal(bson, &s); err != nil {
return s, err
}
return s, nil
}
I can access SessionModel and its type is a map, but when I try to access indexed values, I get the error invalid operation: session.SessionModel["Username"] (type sessions.SessionModel does not support indexing)
. What is going on?
reflect.TypeOf(sessions.SessionModel) //map[string]interface {}
session, err := SessionInstance.Get(sessions.Session{sessions.UserSession{}}, cookie.Value)
username := sessions.SessionModel["Username"] //invalid operation: session.SessionModel["Username"] (type sessions.SessionModel does not support indexing)