- Notifications
You must be signed in to change notification settings - Fork16
Convert Go values to their AST
License
Unknown and 2 other licenses found
Licenses found
Unknown
LICENSEApache-2.0
LICENSE-APACHEMIT
LICENSE-MITNotificationsYou must be signed in to change notification settings
hexops/valast
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Valast converts Go values at runtime into theirgo/ast
equivalent, e.g.:
x:=&foo.Bar{a:"hello world!",B:1.234,}fmt.Println(valast.String(x))
Prints string:
&foo.Bar{a:"hello world!",B:1.234}
This can be useful for debugging and testing, you may think of it as a more comprehensive and configurable version of thefmt
package's%+v
and%#v
formatting directives. It is similar to e.g.repr
in Python.
- Produces Go code via a
go/ast
, defers formatting to the best-in-class Go formattergofumpt. - Fully handles unexported fields, types, and values (optional.)
- Strong emphasis on being used for producing valid Go code that can be copy & pasted directly into e.g. tests.
- Extensively tested, over 88 tests and handling numerous edge cases (such as pointers to unaddressable literal values like
&"foo"
properly, and evenfinding bugs in alternative packages'). - Provide custom AST representations for your types with
valast.RegisterType(...)
.
The following are alternatives to Valast, making note of the differences we found that let us to create Valast:
- github.com/davecgh/go-spew
- may be inactive
- Produces Go-like output, but not Go syntax.
- github.com/shurcooL/go-goon (based on go-spew)
- Produces valid Go syntax, but not via a
go/ast
. - Produces less idiomatic/terse results)
- Was deprecated in favor of valast.
- Produces valid Go syntax, but not via a
- github.com/alecthomas/repr
- Produces Go syntax, but not always valid code (e.g. can emit illegal
&23
, whereas Valast will emit a valid expressionvalast.Addr(23).(int)
), not via ago/ast
. - Does not handle unexported fields/types/values.
- Produces Go syntax, but not always valid code (e.g. can emit illegal
You may also wish to look atautogold andgo-cmp, which aim to solve the "compare Go values in a test" problem.
About
Convert Go values to their AST
Topics
Resources
License
Unknown and 2 other licenses found
Licenses found
Unknown
LICENSEApache-2.0
LICENSE-APACHEMIT
LICENSE-MIT