urlcodec

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2025 License: MIT Imports: 6 Imported by: 1 module, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

urlencoder

Encode/decode nested data structures to/from url.Values using a predictable dotted/recursive syntax.

Install

import "github.com/aatuh/urlencoder"

Quick start

e := urlencoder.NewURLEncoder()

vals, _ := e.Encode(map[string]any{
  "user": map[string]any{
    "id":   1,
    "name": "Ada",
  },
  "tags": []string{"a", "b"},
})
// user.id=1&user.name=Ada&tags[0]=a&tags[1]=b

m, _ := e.Decode(vals)
// m["user"].(map[string]any)["name"] == "Ada"

Rules

  • Keys: a, a.b, a[0], a.mapKey.
  • Structs require json tags for field names; embedded fields are inlined.
  • Maps must have string keys.
  • Pointers/interfaces are dereferenced when non‑nil.

Notes

  • Guardrails: max recursion depth and slice size, plus basic index validation.
  • Decoding uses an internal sparse slice helper and returns regular slices.

Documentation

Overview

Package urlcodec encodes/decodes nested data structures to/from url.Values using a predictable dotted/recursive syntax.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type URLEncoder

type URLEncoder struct{}

URLEncoder encodes and decodes URL values.

func NewURLEncoder

func NewURLEncoder() *URLEncoder

NewURLEncoder returns a new URLEncoder.

Returns:

  • *URLEncoder: The new URLEncoder.

func (URLEncoder) Decode

func (e URLEncoder) Decode(values url.Values) (map[string]any, error)

Decode decodes URL values and supports the following recursive URL syntax: someKey=value someStruct.field=value someSlice[0]=value someMap.key=value

Parameters:

  • values: URL values

Returns:

  • map[string]any: Decoded data
  • error: Error

func (URLEncoder) Encode

func (e URLEncoder) Encode(data map[string]any) (url.Values, error)

Encode encodes URL data and supports the following recursive URL syntax: someKey=value someStruct.field=value someSlice[0]=value someMap.key=value

It will return an error if a "json" tag is not found for a struct field.

Parameters:

  • data: Data to encode

Returns:

  • url.Values: URL values
  • error: Error

Jump to

Keyboard shortcuts

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