From 32d76614aa95707594885416869714f5f73c3375 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Tue, 31 Mar 2015 18:35:36 +0200 Subject: [PATCH] Adds inputHolder --- input_holder.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 input_holder.go diff --git a/input_holder.go b/input_holder.go new file mode 100644 index 00000000..9888e502 --- /dev/null +++ b/input_holder.go @@ -0,0 +1,49 @@ +// Copyright 2014 Manu Martinez-Almeida. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package gin + +type inputHolder struct { + context *Context +} + +func (i inputHolder) FromGET(key string) (va string) { + va, _ = i.fromGET(key) + return +} + +func (i inputHolder) FromPOST(key string) (va string) { + va, _ = i.fromPOST(key) + return +} + +func (i inputHolder) Get(key string) string { + if value, exists := i.fromGET(key); exists { + return value + } + if value, exists := i.fromPOST(key); exists { + return value + } + return "" +} + +func (i inputHolder) fromGET(key string) (string, bool) { + req := i.context.Request + req.ParseForm() + if values, ok := req.Form[key]; ok { + return values[0], true + } else { + return "", false + } +} + +func (i inputHolder) fromPOST(key string) (string, bool) { + req := i.context.Request + req.ParseForm() + if values, ok := req.PostForm[key]; ok { + return values[0], true + } else { + return "", false + } +}