structly

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2025 License: MIT Imports: 6 Imported by: 0

README

structly

Go Reference Go Report Card

A Go static analyzer that enforces required struct fields using custom struct tags.

Overview

structly checks that struct fields marked with required:"true" are explicitly set in struct literals, helping you catch missing field errors at compile time.

Installation

Add to your project:

go get github.com/juanrgon/structly

Usage

1. Mark required fields
type Config struct {
    Host     string `required:"true"`
    Port     int    `required:"true"`
    Database string `required:"true"`
    Timeout  int    // optional
}
2. Integrate with your linting tools
Using go vet

Install the tool:

go install github.com/juanrgon/structly/cmd/structly@latest

Run with go vet:

go vet -vettool=$(which structly) ./...

Add to your Makefile:

.PHONY: vet
vet:
	go vet -vettool=$(which structly) ./...
Using golangci-lint

Create .golangci.yml:

linters-settings:
  govet:
    enable-all: true
    settings:
      printf:
        funcs:
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf

linters:
  enable:
    - govet

Then run:

golangci-lint run --enable=govet
As a library

Import the analyzer in your own tooling:

package main

import (
    "github.com/juanrgon/structly"
    "golang.org/x/tools/go/analysis/singlechecker"
)

func main() {
    singlechecker.Main(structly.Analyzer)
}
3. Fix reported issues

The analyzer will report missing required fields:

example.go:16:6: missing required field "Port" in Config literal
example.go:16:6: missing required field "Database" in Config literal

Invalid code:

config := Config{
    Host: "localhost",
    // Error: missing required fields "Port" and "Database"
}

Fixed code:

config := Config{
    Host:     "localhost",
    Port:     5432,
    Database: "mydb",
}

Integration

CI/CD

GitHub Actions:

- name: Install structly
  run: go install github.com/juanrgon/structly/cmd/structly@latest

- name: Run go vet with structly
  run: go vet -vettool=$(which structly) ./...

Pre-commit hook:

#!/bin/bash
go vet -vettool=$(which structly) ./...

How It Works

The analyzer:

  1. Scans struct composite literals in your code
  2. Checks if the struct has fields with required:"true" tags
  3. Verifies all required fields are explicitly set
  4. Reports missing fields as errors

Built on golang.org/x/tools/go/analysis framework.

Limitations

  • Only checks keyed struct literals (e.g., Config{Host: "x"})
  • Does not check positional literals (e.g., Config{"x", 80})
  • Does not track values through variables or functions

Example

git clone https://github.com/juanrgon/structly.git
cd structly
go install ./cmd/structly
go vet -vettool=$(which structly) ./example

Contributing

Contributions welcome! Please submit issues or pull requests.

License

MIT License - see LICENSE file for details.

Documentation

Overview

Package structly provides a static analyzer that checks for missing required struct fields. Fields marked with `required:"true"` struct tags must be explicitly set in struct literals.

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:     "structly",
	Doc:      "checks that struct fields marked with `required:\"true\"` are set in struct literals",
	Run:      run,
	Requires: []*analysis.Analyzer{inspect.Analyzer},
}

Analyzer is the structly analyzer that checks for missing required fields.

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd
structly command
Package main provides a command-line tool for running the structly analyzer.
Package main provides a command-line tool for running the structly analyzer.
Package plugin provides a golangci-lint module plugin wrapper for structly
Package plugin provides a golangci-lint module plugin wrapper for structly

Jump to

Keyboard shortcuts

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