From a0dee3abdb622c78b2b0c4a127011f16b81ff531 Mon Sep 17 00:00:00 2001 From: kataras Date: Thu, 8 Jun 2017 15:19:04 +0300 Subject: [PATCH] Add link for the "community-maden" middleware I did maintain and upgrade them because community is not evolved too much on these, but that's OK for now. Former-commit-id: b811c133fbfb310834c67d78248f944e0a89c9a6 --- _examples/README.md | 3 ++ .../intermediate/sessions/standalone/main.go | 28 ++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/_examples/README.md b/_examples/README.md index b3ea04de..a0982ee4 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -94,6 +94,9 @@ It doesn't contains "best ways" neither explains all its features. It's just a s * [Online Visitors](advanced/online-visitors/main.go) * [URL Shortener using BoltDB](advanced/url-shortener/main.go) + +You may want to check out examples for jwt, cors and the rest of community-maden middleware by clicking [here](https://github.com/iris-contrib/middleware) + > Do not forget to [star or watch the project](https://github.com/kataras/iris/stargazers) in order to stay updated with the latest tech trends, it takes some seconds for the sake of go! > Developers should read the official [documentation](https://godoc.org/github.com/kataras/iris) in depth, for deep understanding. diff --git a/_examples/intermediate/sessions/standalone/main.go b/_examples/intermediate/sessions/standalone/main.go index d7dfb7d4..f58bc781 100644 --- a/_examples/intermediate/sessions/standalone/main.go +++ b/_examples/intermediate/sessions/standalone/main.go @@ -84,18 +84,38 @@ func main() { // mySessions.DestroyByID // mySessions.DestroyAll - app.Get("/immutable", func(ctx context.Context) { + // remember: slices and maps are muttable by-design + // The `SetImmutable` makes sure that they will be stored and received + // as immutable, so you can't change them directly by mistake. + // + // Use `SetImmutable` consistently, it's slower than `Set`. + // Read more about muttable and immutable go types: https://stackoverflow.com/a/8021081 + app.Get("/set_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 + + // try to change it, if we used `Set` instead of `SetImmutable` this + // change will affect the underline array of the session's value "businessEdit", but now it will not. businessGet[0].Name = "Gabriel" + }) - app.Get("/immutable_get", func(ctx context.Context) { - if ctx.Session().Get("businessEdit").([]businessModel)[0].Name == "Gabriel" { + app.Get("/get_immutable", func(ctx context.Context) { + valSlice := ctx.Session().Get("businessEdit") + if valSlice == nil { + ctx.HTML("please navigate to the /set_immutable first") + return + } + + firstModel := valSlice.([]businessModel)[0] + // businessGet[0].Name is equal to Edward initially + if firstModel.Name != "Edward" { panic("Report this as a bug, immutable data cannot be changed from the caller without re-SetImmutable") } + + ctx.Writef("[]businessModel[0].Name remains: %s", firstModel.Name) + // the name should remains "Edward" })