π 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
Using go
go install github.com/upamune/typereplacer@latest
Using brew
brew install upamune/tap/typereplacer
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:
- "time"
- "github.com/shopspring/decimal"
structs:
- name: "User"
fields:
- name: "Balance"
type: "decimal.Decimal"
- name: "CreatedAt"
type: "time.Time"
- name: "UpdatedAt"
type: "time.Time"
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.