Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use slices of custom type in sessions? #114

Closed
vogxn opened this Issue May 3, 2017 · 10 comments

Comments

Projects
None yet
2 participants
@vogxn
Copy link

commented May 3, 2017

In this rather contrived example I have a custom type Person. A slice of Person values is passed between handlers using sessions. I'm however, unable to determine what should be the type of the val extracted from the session store in line 54. Am I doing something wrong?

val := session.Values["persons"]
// What type is the session value `val`?
if persons, ok := val.([]Person); ok {
	for _, person := range persons {
		fmt.Fprintln(w, person)
	}
} else {
	fmt.Fprintf(w, "invalid type detected: %+v\n", ok)
}
@kisielk

This comment has been minimized.

Copy link
Member

commented May 3, 2017

You should check the error returned by session.Save

@vogxn

This comment has been minimized.

Copy link
Author

commented May 4, 2017

Thanks, should've read the documentation carefully.

Note that in production code, we should check for errors when calling session.Save(r, w), and either display an error message or otherwise handle it.

So, I noticed that []Persons was not registered. Originally I had assumed that gob would be able to Encode/Decode a slice of custom types as shown here. I'm unsure how to proceed because even after registering the slice I'm unable to detect the type.

Here's the altered code: https://play.golang.org/p/sHcjM4fadJ

@kisielk

This comment has been minimized.

Copy link
Member

commented May 4, 2017

@vogxn

This comment has been minimized.

Copy link
Author

commented May 4, 2017

I'm not using any clients to store cookies. I'm storing the values in-memory using the CookieStore provided by gorilla sessions. The save is done as part of a POST handler and I'm trying to use the GET handler to retrieve the saved values.

@kisielk

This comment has been minimized.

Copy link
Member

commented May 4, 2017

The CookieStore is called that for a reason. It stores the session data in a secure cookie on the client.

@vogxn

This comment has been minimized.

Copy link
Author

commented May 4, 2017

Ah, alright. So, am I going wrong with the CookieStore send?

@kisielk

This comment has been minimized.

Copy link
Member

commented May 4, 2017

It's fine if your client uses cookies, but otherwise you may want to use FileSystemStore or one of the database based stores.

@vogxn

This comment has been minimized.

Copy link
Author

commented May 5, 2017

So I converted the code in the gist to use the Filesystemstore and still find that type assertion doesn't work.

 58   val := session.Values["persons"]
 59   // What type is the session value `val`?
 60   if persons, ok := val.([]Person); ok {
 61     for _, person := range persons {
 62       fmt.Fprintln(w, person)
 63     }
 64   } else {
 65     fmt.Println(reflect.TypeOf(persons))
 66     fmt.Fprintf(w, "invalid type detected: %+v\n", ok)
 67   }

On Line 65, I plugged in some code to detect the type of persons and found that the reflect package correctly detects the type as []main.Person and yet, the type assertion check fails.

@kisielk

This comment has been minimized.

Copy link
Member

commented May 5, 2017

Sorry, I realized my advice is not fully complete. Even FilesystemStore requires the use of a cookie, it's just that the data of the session itself is not stored in the cookie, just a session identifier. I think pretty much all the store implementations require the use of a cookie so they can identify the client when it returns to the site.

@vogxn

This comment has been minimized.

Copy link
Author

commented May 6, 2017

Thanks @kisielk for your patience - I'm new to web applications so this is my mistake. I should've sent a cookie back to the client and later looked it up in my cookie store. I was following the examples on the sessions package and was misled. I believe the documentation is clear for someone with a background in writing sessions based applications. I can't quite tell.

Thanks again. Closing.

@vogxn vogxn closed this May 6, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.