structx

package
v0.11.4 Latest Latest
Warning

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

Go to latest
Published: May 28, 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.

Field names are derived from json struct tags, falling back to the Go field name when no tag is present. Fields tagged with json:"-" are skipped.

Usage:

m, err := structx.ToMap(myStruct)
result, err := structx.FromMap(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(data map[string]any, target reflect.Type) (any, error)

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

func Marshal

func Marshal(input any) map[string]any

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

func ToMap

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

ToMap converts a struct to map[string]any using json tag field names. It returns ErrInvalidStructInput if input is nil, a nil pointer, or not a struct type.

func Unmarshal

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

Unmarshal converts map[string]any to a struct of the given type. It returns ErrTargetTypeMustBeStruct if typ is not a struct type. Fields are matched by json 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