typereplacer

module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2024 License: MIT

README ΒΆ

πŸ”„ typereplacer

✨ A Go struct field type rewriter

🎯 What it does

typereplacer is a CLI tool that helps you refactor Go struct field types across your entire codebase with ease. Simply define your type changes in a YAML config, and let the tool do the heavy lifting!

✨ Key Features
  • πŸ› οΈ Simple YAML Configuration - Define your type changes in a clear, structured way
  • πŸ“¦ Bulk Updates - Change multiple struct fields across many files at once

πŸš€ Getting Started

Installation
go install github.com/upamune/typereplacer@latest
Usage
typereplacer --config=./myconfig.yaml ./path/to/pkg

πŸ“ Configuration

Create a YAML config file that specifies your desired type changes:

# yaml-language-server: $schema=https://raw.githubusercontent.com/upamune/typereplacer/refs/tags/v0.2.0/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
imports:
  - "fmt"
  - "strings"
structs:
  - name: "MyStruct"
    fields:
      - name: "Value"
        type: "int"
  - name: "YourStruct"
    fields:
      - name: "Text"
        type: "string"
Config Structure
  • imports: List of packages to analyze (used for type resolution)
  • structs: List of struct definitions to modify
    • name: The struct name to target
    • fields: List of fields to update
      • name: Field name to modify
      • type: New type to apply

πŸ“‹ Example

Let's say you have this initial code:

type User struct {
    ID        int
    Balance   float64
    CreatedAt string
    UpdatedAt string
}

And you want to change the time fields to use time.Time and the balance field to use decimal.Decimal. Create a config file typeconfig.yaml:

imports:
  - "time"
  - "github.com/shopspring/decimal"
structs:
  - name: "User"
    fields:
      - name: "Balance"
        type: "decimal.Decimal"
      - name: "CreatedAt"
        type: "time.Time"
      - name: "UpdatedAt"
        type: "time.Time"

Run the command:

typereplacer --config=typeconfig.yaml ./...

The tool will update your code to:

type User struct {
    ID        int
    Balance   decimal.Decimal
    CreatedAt time.Time
    UpdatedAt time.Time
}

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.

Directories ΒΆ

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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