Skip to content

flaticols/structfieldsenforcer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

structfieldsenforcer

A Go static analysis tool that enforces mandatory struct field initialization using struct tags.

Installation

go install github.com/flaticols/structfieldsenforcer@latest

Usage

Mark struct fields as mandatory using tags:

type Config struct {
    DatabaseURL string `mandatory:""`
    APIKey      string `mandatory:""`
    Port        int    `mandatory:""`
    Debug       bool   // Optional field
}

The tool detects violations like:

config := Config{
    DatabaseURL: "postgres://localhost/mydb",
    // Missing APIKey and Port
}

Quick Start

# Check for violations
structfieldsenforcer ./...

# Fix violations by adding TODO comments
structfieldsenforcer -fix ./...

# Generate missing constructors
structfieldsenforcer -fix -constructors=gen ./...

# Update existing constructors
structfieldsenforcer -fix -constructors=update ./...

Fix Examples

Before fixing:

config := Config{
    DatabaseURL: "postgres://localhost/mydb",
    Debug:       true,
}

After structfieldsenforcer -fix:

config := Config{
    DatabaseURL: "postgres://localhost/mydb",
    Debug:       true,
    APIKey:      // TODO: provide value for APIKey,
    Port:        // TODO: provide value for Port,
}

After structfieldsenforcer -fix -constructors=gen:

Important

The analyzer intentionally creates compilation errors with TODO comments to ensure you don't miss mandatory fields that need values.

config := Config{
    DatabaseURL: "postgres://localhost/mydb",
    Debug:       true,
    APIKey:      // TODO: provide value for APIKey,
    Port:        // TODO: provide value for Port,
}

func NewConfig(apikey string, databaseURL string, port int) *Config {
    return &Config{APIKey: apikey, DatabaseURL: databaseURL, Port: port}
}

Constructor Update Example:

If you have an existing constructor missing mandatory fields:

func NewConfig(databaseURL string) *Config {
    return &Config{DatabaseURL: databaseURL}
}

After structfieldsenforcer -fix -constructors=update:

func NewConfig(apikey string, databaseURL string, port int) *Config {
    return &Config{APIKey: apikey, DatabaseURL: databaseURL, Port: port}
}

Commands

Basic Usage

  • structfieldsenforcer ./... - Check for violations in all packages
  • structfieldsenforcer -fix ./... - Fix violations by adding TODO comments
  • structfieldsenforcer main.go - Check specific file

Constructor Operations

  • structfieldsenforcer -constructors=gen ./... - Check what constructors can be generated
  • structfieldsenforcer -constructors=update ./... - Check what constructors can be updated
  • structfieldsenforcer -constructors=gen,update ./... - Check both operations

Fixing with Constructors

  • structfieldsenforcer -fix -constructors=gen ./... - Fix violations AND generate constructors
  • structfieldsenforcer -fix -constructors=update ./... - Fix violations AND update existing constructors
  • structfieldsenforcer -fix -constructors=gen,update ./... - Fix violations, generate AND update constructors

Advanced Usage

  • structfieldsenforcer -tag=required ./... - Use custom tag name
  • structfieldsenforcer -constructor-patterns="New*,Build*,Make*" ./... - Custom constructor patterns
  • structfieldsenforcer -fix -tag=required -constructors=gen -constructor-patterns="Build*" ./pkg/... - Complex combination

Options

Core Flags

  • -tag string - Custom tag name for mandatory fields (default: "mandatory")
    Example: -tag=required to use required:"" instead of mandatory:""

  • -fix - Apply fixes to files. Without this flag, only reports violations without modifying files
    Example: -fix enables file modifications

  • -help - Show help message and exit
    Example: -help displays usage information

Constructor Operations

  • -constructors string - Constructor operations to perform:

    • gen - Generate missing constructors for structs with violations
    • update - Update existing constructors to include all mandatory fields
    • gen,update - Both generate and update constructors

    Important: Constructor operations only modify files when combined with -fix. Without -fix, they only report what would be done.

  • -constructor-patterns string - Comma-separated patterns to identify constructor functions (default: "New*,Create*")

    • Supports glob patterns with * wildcard
    • Examples: "New*", "Create*", "Build*", "New*,Create*,Build*"
    • Functions matching these patterns will be treated as constructors

Command Line Reference

Package Patterns

The tool accepts Go package patterns as arguments:

  • ./... - Recursively analyze all packages in current directory (default)
  • ./pkg/... - Recursively analyze packages in pkg directory
  • file.go - Analyze specific file
  • pkg1 pkg2 - Analyze multiple specific packages
  • . - Analyze current directory only (non-recursive)

Exit Codes

  • 0 - Success, no violations found
  • 1 - Violations found (or would be found in check-only mode)
  • 2 - Syntax or parsing errors in Go files
  • 3 - File system errors (permissions, file not found, etc.)

Flag Combinations

Flags Behavior
(none) Check for violations, exit with code 1 if found
-fix Fix violations by adding TODO comments
-constructors=gen Check what constructors can be generated
-constructors=update Check what constructors can be updated
-fix -constructors=gen Fix violations AND generate missing constructors
-fix -constructors=update Fix violations AND update existing constructors
-fix -constructors=gen,update Fix violations, generate AND update constructors

Key Rule: Constructor operations (-constructors) only modify files when -fix is also specified.

License

MIT

About

A Go static analysis tool that enforces mandatory struct field initialization using struct tags.

Resources

License

Stars

Watchers

Forks