https://github.com/mitchs-dev/build-struct
Convert JSON/YAML -> GO struct
https://github.com/mitchs-dev/build-struct
Last synced: about 2 months ago
JSON representation
Convert JSON/YAML -> GO struct
- Host: GitHub
- URL: https://github.com/mitchs-dev/build-struct
- Owner: mitchs-dev
- License: mit
- Created: 2024-09-03T15:50:25.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-03-27T09:56:20.000Z (about 2 months ago)
- Last Synced: 2025-03-27T10:38:23.534Z (about 2 months ago)
- Language: Go
- Size: 26.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# build-struct
This is a simple command line tool which converts a given configuration file into a Go struct. It accepts a configuration file in JSON or YAML format. The tool looks at the content within the configuration file, not necessarily the file extension. I designed it this way as sometimes I use `config` instead of `config.yaml` so I wanted it to be flexible.
## Use Case
For my personal use case, I have structs which have a lot of fields and I have to manually create them. This tool helps me to automate the process of creating structs.
## Installation
```bash
go install github.com/mitchs-dev/build-struct@latest
```## Usage
```bash
build-struct
```## Example
I have a configuration file (`config.yaml`) with the following content:
```yaml
apiVersion: v1
metadata:
name: my-app
namespace: default
settings:
logging:
debug: false
format: json
database:
host: localhost
port: 5432
username: root
password: password
data:
- path: /var/data
threshold: 0.80
enable: true
size: 100Gi
- path: /var/log
threshold: 0.90
enable: true
size: 10Gi
```I can run the following command:
```bash
build-struct Config config.yaml
```This will generate the following Go struct:
```go
type Config struct {
ApiVersion string `yaml:"apiVersion"`
Metadata struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
} `yaml:"metadata"`
Settings struct {
Logging struct {
Debug bool `yaml:"debug"`
Format string `yaml:"format"`
} `yaml:"logging"`
Database struct {
Password string `yaml:"password"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Username string `yaml:"username"`
} `yaml:"database"`
Data []struct {
Threshold float64 `yaml:"threshold"`
Enable bool `yaml:"enable"`
Size string `yaml:"size"`
Path string `yaml:"path"`
} `yaml:"data"`
} `yaml:"settings"`
}
```## Caveats
- `[]byte` values are a bit tricky to handle. If you need to support `[]byte` values, you will have to manually update the generated struct. The suggested approach is to use `string` instead of `[]byte` in the configuration file.
## Contributing
If you have any suggestions or improvements, feel free to open an issue or a pull request.