Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/uudashr/structfield
Go linter to check the struct literal to use field name
https://github.com/uudashr/structfield
Last synced: about 2 months ago
JSON representation
Go linter to check the struct literal to use field name
- Host: GitHub
- URL: https://github.com/uudashr/structfield
- Owner: uudashr
- License: mit
- Created: 2021-08-02T12:37:46.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-08-06T09:20:38.000Z (over 3 years ago)
- Last Synced: 2024-06-20T12:38:10.627Z (7 months ago)
- Language: Go
- Size: 10.7 KB
- Stars: 6
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![GoDoc](https://godoc.org/github.com/uudashr/structfield?status.svg)](https://godoc.org/github.com/uudashr/structfield)
# Structfield
Find struct literals using non-labeled fields.The structfield analysis reports the usage of struct literal using non-labeled fields more than defined limit (default: 2). The non-labeled field above the limit considered has higher cognitive load (harder to understand and rememeber).
## Understanding Struct Literal
Given code, variable assigned using struct literal:
```go
acc := Account{
"[email protected]",
"John Smith",
[]Permission{
Permission{"account", "read"},
Permission{"account", "write"},
},
true,
false,
}
```Above code is harder to understand, hard to guess the field name since we have to remember exact order of the fields. The workaround is you have to always look the declaration of the `Account` type.
Suggestion is to refactor the code to:
```go
acc := Account{
Email: "[email protected]",
Name: "John Smith",
Permission: []Permission{
Permission{"account", "read"}, // Non-labeled here is still ok
Permission{"account", "write"},
},
Verified: true,
Deactivated: false,
}
```The limit set to 2, which considered easy to understand and remember.
## Benefits
By using the labeled fields you several benefits1. The fields doesn't have to be in order
2. You don't have to declare the value if it's a default valueExample:
```go
acc := Account{
Email: "[email protected]",
Name: "John Smith",
Permission: []Permission{
Permission{"account", "read"}, // Non-labeled here is still ok
Permission{"account", "write"},
},
Verified: true,
Deactivated: false,
}
```can be simplified into:
```go
acc := Account{
Name: "John Smith", // `Name` come first
Email: "[email protected]",
Permission: []Permission{
Permission{"account", "read"}, // Non-labeled here is still ok
Permission{"account", "write"},
},
Verified: true,
// Remove the `Deactivated: false` since it use default value
}
```## Installation
```
$ go install github.com/uudashr/structfield/cmd/structfield@latest
```or
```
$ go get github.com/uudashr/structfield/cmd/structfield
```## Usage
```
$ structfield -limit 2 testdata/src/a/*.go
testdata/src/a/a.go:20:9: Found 4 non-labeled fields on struct literal (> 2)
```