https://github.com/mikeschinkel/go-typegen
Simple Golang generator package for test fixtures
https://github.com/mikeschinkel/go-typegen
code-generation go golang test testing testing-tools tests
Last synced: over 1 year ago
JSON representation
Simple Golang generator package for test fixtures
- Host: GitHub
- URL: https://github.com/mikeschinkel/go-typegen
- Owner: mikeschinkel
- License: apache-2.0
- Created: 2023-11-05T14:09:52.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-14T06:16:12.000Z (over 2 years ago)
- Last Synced: 2025-03-23T22:45:46.561Z (over 1 year ago)
- Topics: code-generation, go, golang, test, testing, testing-tools, tests
- Language: Go
- Homepage:
- Size: 126 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-typegen
Simple generator of Golang code to instantiate a type given an instantiated object.
## Intended Use-case
To create type instantiation code for use in unit testing from within code that generates over complex objects that would be hard to manually convert to use in testing.
**_NOTE:_** While good testing practices create tests with small data targeted specific edge-cases, `typegen` is not intended for typical unit testing. `typegen` is for diagnosing a bug in a huge data structure where it is hard to determine what part of the data is causing error. Perfect examples for this are heavily recursive data structures, which typegen should handle with aplomb.
## Usage
```go
package main
import "github.com/mikeschinkel/go-typegen"
func main() {
value := []int{1, 2, 3}
funcName := "getData"
// Replace w/package name where you will use getdata() func.
omitPkg := "typegen_test"
m := typegen.NewNodeMarshaler(value)
nodes := m.Marshal()
b := typegen.NewCodeBuilder(funcName, omitPkg, nodes)
code := b.String()
println(code)
}
```
The above code will print the following:
```go
func getData() []int {
var1 := []int{1,2,3,}
return var1
}
```
See it run [in the playground](https://goplay.tools/snippet/7SOrqjjpQTj).
## Stability
This is brand new and likely has many rough edges.
However, I'd like to make it robust so if you find issues either submit a PR or a bug report, please.
## FAQ
- Q: What not just use `fmt.Sprintf("%#v", value)`?
- A: Because it does not recurse through complex structures.
----
- Q: Your question goes [here](https://github.com/mikeschinkel/go-typegen/issues/new).
- A: ???
## License
Apache 2.0