Skip to content

agentine/structify

Repository files navigation

structify

CI Go Reference

A modern Go library for struct utilities — convert structs to maps, extract field names and values, inspect and mutate fields. Drop-in replacement for github.com/fatih/structs.

Install

go get github.com/agentine/structify

Usage

package main

import (
	"fmt"
	"github.com/agentine/structify"
)

type Server struct {
	Name    string `json:"name"`
	ID      int    `json:"id"`
	Enabled bool   `json:"enabled"`
}

func main() {
	s := Server{Name: "web", ID: 1, Enabled: true}

	// Convert struct to map
	m := structify.Map(s)
	fmt.Println(m) // map[Name:web ID:1 Enabled:true]

	// Get field names
	names := structify.Names(s)
	fmt.Println(names) // [Name ID Enabled]

	// Get field values
	values := structify.Values(s)
	fmt.Println(values) // [web 1 true]

	// Use custom tag for map keys
	st := structify.New(s, structify.WithTag("json"))
	m = st.Map()
	fmt.Println(m) // map[name:web id:1 enabled:true]
}

API

Core Functions

Function Description
New(s any, opts ...Option) *Struct Wrap a struct value
Map(s any) map[string]any Convert struct to map
Values(s any) []any Extract field values
Names(s any) []string Extract field names
Fields(s any) []*Field Get all fields
IsZero(s any) bool Check if all fields are zero
HasZero(s any) bool Check if any field is zero
IsStruct(s any) bool Check if value is a struct
Name(s any) string Get struct type name

Field Methods

Method Description
Name() string Field name
Value() any Field value
Kind() reflect.Kind Field kind
Tag(key string) string Get struct tag value
IsZero() bool Check if zero value
IsExported() bool Check if exported
IsEmbedded() bool Check if embedded
Set(val any) error Set field value
Fields() []*Field Nested struct fields

Options

Option Description
WithTag(tag string) Use custom struct tag for map keys (default: field name)

Migration from fatih/structs

structify is a drop-in replacement for fatih/structs. Key differences:

  1. any instead of interface{} — Modern Go style throughout.
  2. Error returnsStruct.Field(name) returns (*Field, error) instead of panicking.
  3. Configurable tagsNew(s, WithTag("json")) to use any struct tag for map keys.
  4. Nil pointer safety — Graceful handling of nil embedded pointers.

Quick migration

- import "github.com/fatih/structs"
+ import "github.com/agentine/structify"

- m := structs.Map(s)
+ m := structify.Map(s)

- f := structs.Fields(s)
+ f := structify.Fields(s)

The compatibility layer provides FillMap and other functions matching the original API signatures.

License

MIT

About

Modern Go struct utilities — convert structs to maps, inspect and mutate fields. Drop-in replacement for fatih/structs.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors