pickmap

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2025 License: MIT Imports: 7 Imported by: 1

README

pickmap

Extract typed request data from url, body, header(s), and cookie(s) into a map[string]any using a declarative field config. Supports nested objects, slices, defaults, optional fields, and simple type conversion.

Install

import "github.com/aatuh/pickmap"

Quick start

// Minimal URL decoder: first value per key.
type formDecoder struct{}
func (formDecoder) Decode(v url.Values) (map[string]any, error) {
  m := make(map[string]any, len(v))
  for k, vals := range v {
    if len(vals) > 0 { m[k] = vals[0] }
  }
  return m, nil
}

// Optional conversions. Keys are semantic type names used in config.
conv := map[string]func(any) any{
  "int64": func(x any) any {
    switch t := x.(type) {
    case string:
      if n, err := strconv.ParseInt(t, 10, 64); err == nil { return n }
    }
    return nil
  },
}

picker := pickmap.NewPickmap(formDecoder{}, conv)

cfg := &pickmap.MapFieldConfig{
  Fields: map[string]*pickmap.MapFieldConfig{
    "id":    { Source: "url",   ExpectedType: "int64" },
    "name":  { Source: "body" },
    "token": { Source: "header", Optional: true },
  },
}

m, err := picker.PickMap(r, cfg)
if err != nil { /* handle */ }

fmt.Printf("id=%v name=%v token=%v\n", m["id"], m["name"], m["token"])

Sources and defaults

  • url, body, header/headers, cookie/cookies
  • Default source by method: GETurl, others → body
  • A parent config Source cascades to children unless a child sets its own Source.

Nested fields and slices

  • If the raw body contains nested objects or arrays, define Fields for that key; nested maps are traversed accordingly.
  • If raw data is flat, composite keys like parent.child are supported to pick nested pieces.
  • Slices of nested objects are supported; each element is processed.

Types, defaults, and optional

  • ExpectedType converts values. Built‑ins: int64, int, float64, bool. You can add more via the conversionMap passed to the picker.
  • DefaultValue is applied when a value is missing.
  • Optional: true skips missing values instead of emitting empty ones.

Notes

  • Body is decoded from JSON with UseNumber to preserve numeric range; the request body is restored for downstream handlers.
  • Unknown Source values cause a panic to surface misconfiguration.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MapFieldConfig

type MapFieldConfig struct {
	Source       string
	ExpectedType string
	DefaultValue any
	Optional     bool
	Fields       map[string]*MapFieldConfig
}

MapFieldConfig lets define the mapping for extracting a map input.

  • Source: if set at a level, it will override any field-level sources in the children.
  • ExpectedType: if set for a leaf field, the value is converted to that type. For example, "int64", "int", "float64", etc.

type Pickmap

type Pickmap struct {
	// contains filtered or unexported fields
}

Pickmap is generic over T which may be a struct or a map.

func NewPickmap

func NewPickmap(
	urlDecoder URLDecoder, conversionMap map[string]func(any) any,
) *Pickmap

NewPickmap returns a new Pickmap.

Parameters:

  • urlDecoder: The URL decoder.
  • conversionMap: A map of conversion functions.

Returns:

  • *Pickmap: The new Pickmap.

func (*Pickmap) PickMap

func (o *Pickmap) PickMap(
	r *http.Request, config *MapFieldConfig,
) (map[string]any, error)

PickMap extracts a map[string]any from the request based on the provided MapFieldConfig. It supports both nested and flat extraction. If a source is set at a higher (parent) level, then child fields will use that source.

Parameters:

  • r: The HTTP request.
  • config: The MapFieldConfig.

Returns:

  • map[string]any: The extracted map.
  • error: Any error that occurred during processing.

type URLDecoder

type URLDecoder interface {
	Decode(values url.Values) (map[string]any, error)
}

URLDecoder interface for decoding URL values.

Jump to

Keyboard shortcuts

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