structx

package
v0.11.7 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 3 Imported by: 0

README

StructX

Lightweight utilities for converting between Go structs and map[string]any.

Features

  • Marshal – Convert a struct (or pointer to struct) to map[string]any.
  • ToMap – Same as Marshal but returns an error when input is nil or not a struct.
  • Unmarshal / FromMap – Populate a struct instance from map[string]any using reflection.
  • Respects json tags and skips unexported or json:"-" fields.

Quick Start

package main

import (
    "fmt"
    "reflect"

    "github.com/kaptinlin/gozod/pkg/structx"
)

type User struct {
    Name  string `json:"name"`
    Email string `json:"email"`
    Age   int    `json:"age"`
}

func main() {
    user := User{"John Doe", "john@example.com", 30}

    // struct → map
    userMap := structx.Marshal(user)
    fmt.Println(userMap)

    // struct → map with error handling
    userMap2, err := structx.ToMap(user)
    fmt.Println(userMap2, err)

    // map → struct
    restored, _ := structx.Unmarshal(userMap, reflect.TypeOf(User{}))
    fmt.Println(restored)
}

JSON Tag Support

structx automatically honors json tags for field names and omits fields tagged with -:

type APIResponse struct {
    UserID   int    `json:"user_id"`
    UserName string `json:"username"`
    IsActive bool   `json:"is_active"`
    Secret   string `json:"-"` // skipped
}

data := structx.Marshal(APIResponse{1, "alice", true, "hidden"})
// map[user_id:1 username:alice is_active:true]

Documentation

Overview

Package structx converts between Go structs and map[string]any.

Every function takes the struct tag used for field names as its first argument (e.g. "json", "yaml", "toml"). Field names fall back to the Go field name when no such tag is present, and fields tagged with `tag:"-"` are skipped.

Usage:

m, err := structx.ToMap("json", myStruct)
result, err := structx.FromMap("yaml", m, reflect.TypeOf(MyStruct{}))

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidStructInput indicates the input is not a struct or is nil.
	ErrInvalidStructInput = errors.New("input is not a struct or is nil")
	// ErrTargetTypeMustBeStruct indicates the target type is not a struct.
	ErrTargetTypeMustBeStruct = errors.New("target type must be struct")
)

Sentinel errors for structx operations.

Functions

func FromMap

func FromMap(fieldNameTag string, data map[string]any, target reflect.Type) (any, error)

FromMap converts map[string]any to a struct of the given type using the named field-name tag. It returns ErrTargetTypeMustBeStruct if target is not a struct type. Fields are matched by tag name, falling back to the Go field name.

func Marshal

func Marshal(fieldNameTag string, input any) map[string]any

Marshal converts a struct to map[string]any using the named field-name tag for field names. Marshal returns nil if input is nil, a nil pointer, or not a struct.

func ToMap

func ToMap(fieldNameTag string, input any) (map[string]any, error)

ToMap converts a struct to map[string]any using the named field-name tag (e.g. "json", "yaml", "toml") for field names. It returns ErrInvalidStructInput if input is nil, a nil pointer, or not a struct type.

func Unmarshal

func Unmarshal(fieldNameTag string, data map[string]any, typ reflect.Type) (any, error)

Unmarshal converts map[string]any to a struct of the given type using the named field-name tag. It returns ErrTargetTypeMustBeStruct if typ is not a struct type. Fields are matched by tag name, falling back to the Go field name.

Types

This section is empty.

Jump to

Keyboard shortcuts

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