Help us shape the future of Go! Take the 2026 Go Developer Survey

restmap

package
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultMaxMemory = 32 << 20 // 32 MB as same as http.Request

Functions

func Decode

func Decode(dest any, r *http.Request) error
Example
package main

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"

	"github.com/future-architect/tagscanner/examples/restmap"
)

func main() {
	r := createRequest()

	type Request struct {
		Method  string                `rest:"method"`
		Auth    string                `rest:"header:Authorization"`
		TraceID string                `rest:"cookie:trace-id"`
		Title   string                `rest:"body:title-field"`
		File    multipart.File        `rest:"body:file-field"`
		Header  *multipart.FileHeader `rest:"body:file-field"`
		Ctx     context.Context       `rest:"context"`
	}

	var req Request

	err := restmap.Decode(&req, r)
	fmt.Printf("err: %v\n", err)
	fmt.Printf("method: %s\n", req.Method)
	fmt.Printf("trace-id from cookie: %s\n", req.TraceID)
	fmt.Printf("title from form: %s\n", req.Title)
	fmt.Printf("uploaded file name from form: %s\n", req.Header.Filename)
	content, _ := ioutil.ReadAll(req.File)
	defer req.File.Close()
	fmt.Printf("uploaded file content from form: %s\n", string(content))
	fmt.Printf("value of context: %s\n", req.Ctx.Value("context-key").(string))
}

func createRequest() *http.Request {
	var b bytes.Buffer
	w := multipart.NewWriter(&b)
	writer, _ := w.CreateFormField("title-field")
	io.WriteString(writer, "secret file")
	writer, _ = w.CreateFormFile("file-field", "secret.txt")
	io.WriteString(writer, "file content")
	w.Close()

	ctx := context.WithValue(context.Background(), "context-key", "context-value")

	req, _ := http.NewRequestWithContext(ctx, "POST", "http://example.com", &b)
	req.Header.Set("Content-Type", w.FormDataContentType())
	req.Header.Set("Authorization", "session-key")
	req.Header.Set("Cookie", "trace-id=12345")

	return req
}
Output:
err: <nil>
method: POST
trace-id from cookie: 12345
title from form: secret file
uploaded file name from form: secret.txt
uploaded file content from form: file content
value of context: context-value

Types

type FieldType

type FieldType string
const (
	MethodField  FieldType = "method"
	PathField    FieldType = "path"
	HeaderField  FieldType = "header"
	CookieField  FieldType = "cookie"
	QueryField   FieldType = "query"
	BodyField    FieldType = "body"
	ContextField FieldType = "context"
	IgnoreField  FieldType = "-"
)

type RestTag

type RestTag struct {
	Type     FieldType
	EType    reflect.Type
	Name     string
	Default  string
	Optional bool
	Base     bool
}

func ParseRestTag

func ParseRestTag(fieldName, tagSource, fullPath string, eType reflect.Type) (*RestTag, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL