From 6146dc09abeb502f7fab6c14f58c228d4b141970 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Fri, 1 Feb 2019 13:59:29 +0200 Subject: [PATCH] version 1.0.7 with an exported muxie.ResponseWriter that end-devs can wrap to custom http response writers --- LICENSE | 2 +- README.md | 2 +- doc.go | 2 +- params_writer.go | 43 ++++++++++++++++++++++++++++++------------- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/LICENSE b/LICENSE index 7ef5f6d..a38ff5c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2018 Gerasimos Maropoulos +Copyright (c) 2018-2019 Gerasimos Maropoulos Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 7bb16bf..aab2090 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@
- Release/stability diff --git a/doc.go b/doc.go index 736c1e5..98443c4 100644 --- a/doc.go +++ b/doc.go @@ -5,7 +5,7 @@ Source code and other details for the project are available at GitHub: Current Version -1.0.4 +1.0.7 Installation diff --git a/params_writer.go b/params_writer.go index fe0d7d3..023eefc 100644 --- a/params_writer.go +++ b/params_writer.go @@ -11,31 +11,31 @@ import ( // then the `GetParam("name")` will return the value of "kataras". // If not associated value with that key is found then it will return an empty string. // -// The function will do its job only if the given "w" http.ResponseWriter interface is an `paramsWriter`. +// The function will do its job only if the given "w" http.ResponseWriter interface is an `ResponseWriter`. func GetParam(w http.ResponseWriter, key string) string { - if store, ok := w.(*paramsWriter); ok { + if store, ok := w.(ResponseWriter); ok { return store.Get(key) } return "" } -// GetParams returns all the available parameters based on the "w" http.ResponseWriter which should be a *paramsWriter. +// GetParams returns all the available parameters based on the "w" http.ResponseWriter which should be a ResponseWriter. // -// The function will do its job only if the given "w" http.ResponseWriter interface is an `paramsWriter`. +// The function will do its job only if the given "w" http.ResponseWriter interface is an `ResponseWriter`. func GetParams(w http.ResponseWriter) []ParamEntry { - if store, ok := w.(*paramsWriter); ok { - return store.params + if store, ok := w.(ResponseWriter); ok { + return store.GetAll() } return nil } -// SetParam sets manually a parameter to the "w" http.ResponseWriter which should be a *paramsWriter. +// SetParam sets manually a parameter to the "w" http.ResponseWriter which should be a ResponseWriter. // This is not commonly used by the end-developers, // unless sharing values(string messages only) between handlers is absolutely necessary. func SetParam(w http.ResponseWriter, key, value string) bool { - if store, ok := w.(*paramsWriter); ok { + if store, ok := w.(ResponseWriter); ok { store.Set(key, value) return true } @@ -43,17 +43,29 @@ func SetParam(w http.ResponseWriter, key, value string) bool { return false } -type paramsWriter struct { - http.ResponseWriter - params []ParamEntry -} - // ParamEntry holds the Key and the Value of a named path parameter. type ParamEntry struct { Key string Value string } +// ResponseWriter is the muxie's specific ResponseWriter to hold the path parameters. +// Usage: use this to cast a handler's `http.ResponseWriter` and pass it as an embedded parameter to custom response writer +// that will be passed to the next handler in the chain. +type ResponseWriter interface { + http.ResponseWriter + ParamsSetter + Get(string) string + GetAll() []ParamEntry +} + +type paramsWriter struct { + http.ResponseWriter + params []ParamEntry +} + +var _ ResponseWriter = (*paramsWriter)(nil) + // Set implements the `ParamsSetter` which `Trie#Search` needs to store the parameters, if any. // These are decoupled because end-developers may want to use the trie to design a new Mux of their own // or to store different kind of data inside it. @@ -84,6 +96,11 @@ func (pw *paramsWriter) Get(key string) string { return "" } +// GetAll returns all the path parameters keys-values. +func (pw *paramsWriter) GetAll() []ParamEntry { + return pw.params +} + func (pw *paramsWriter) reset(w http.ResponseWriter) { pw.ResponseWriter = w pw.params = pw.params[0:0]