Skip to content

Commit

Permalink
version 1.0.7 with an exported muxie.ResponseWriter that end-devs can…
Browse files Browse the repository at this point in the history
… wrap to custom http response writers
  • Loading branch information
kataras committed Feb 1, 2019
1 parent 8720761 commit 6146dc0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2018 Gerasimos Maropoulos <[email protected]>
Copyright (c) 2018-2019 Gerasimos Maropoulos <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div align="center">
<!-- Release -->
<a href="https://github.com/kataras/muxie/releases">
<img src="https://img.shields.io/badge/release%20-v1.0.6-0077b3.svg?style=flat-squaree"
<img src="https://img.shields.io/badge/release%20-v1.0.7-0077b3.svg?style=flat-squaree"
alt="Release/stability" />
</a>
<!-- Godocs -->
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 30 additions & 13 deletions params_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,61 @@ 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
}

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.
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 6146dc0

Please sign in to comment.