Skip to content
/ pickmap Public

Extract structured maps from HTTP requests in Go. Define field configs with sources (URL, body, headers, cookies), types, defaults, and optional flags, and get back a clean map[string]any.

License

Notifications You must be signed in to change notification settings

aatuh/pickmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Extract structured maps from HTTP requests in Go. Define field configs with sources (URL, body, headers, cookies), types, defaults, and optional flags, and get back a clean map[string]any.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages