From 8b66c2de3b9e9d0c4c7ee2e72afe4cd39a401e83 Mon Sep 17 00:00:00 2001 From: kataras Date: Thu, 8 Jun 2017 14:39:55 +0300 Subject: [PATCH] Add a trivial sessions example of `SetImmutable`. https://github.com/iris-contrib/community-board/issues/5#issuecomment-307030072 Former-commit-id: 2d6267465cceeba31c96feba19f4851d4db7f7aa --- .../intermediate/sessions/standalone/main.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/_examples/intermediate/sessions/standalone/main.go b/_examples/intermediate/sessions/standalone/main.go index 4ba505a2..d7dfb7d4 100644 --- a/_examples/intermediate/sessions/standalone/main.go +++ b/_examples/intermediate/sessions/standalone/main.go @@ -8,8 +8,8 @@ import ( "github.com/kataras/iris/sessions" ) -type myobject struct { - name string +type businessModel struct { + Name string } func main() { @@ -84,5 +84,20 @@ func main() { // mySessions.DestroyByID // mySessions.DestroyAll + app.Get("/immutable", func(ctx context.Context) { + business := []businessModel{{Name: "Edward"}, {Name: "value 2"}} + ctx.Session().SetImmutable("businessEdit", business) + businessGet := ctx.Session().Get("businessEdit").([]businessModel) + // businessGet[0].Name is equal to Edward initially + businessGet[0].Name = "Gabriel" + }) + + app.Get("/immutable_get", func(ctx context.Context) { + if ctx.Session().Get("businessEdit").([]businessModel)[0].Name == "Gabriel" { + panic("Report this as a bug, immutable data cannot be changed from the caller without re-SetImmutable") + } + // the name should remains "Edward" + }) + app.Run(iris.Addr(":8080")) }