go-jsonnet/dump/README.md
Liang Mingqiang aeda6138e5 experiment dump ast to source code (#101)
* Add facility to dump part of Go heap as Go code.

* Generate stdlib AST as Go code

* Use hardcoded stdlib AST instead of parsing it
2017-11-07 23:31:11 -05:00

72 lines
956 B
Markdown

# dump
Package dump can dump a Go data structure to Go source file, so that it can be statically embeded into other code.
# Project Status
* UnsafePointer is not supported yet
* Cycles in the object graph are not supported yet
# Implementation Notes
## Aliasing
Aliases to primitives and structs will be preserved in the objects described by the generated code.
### Primitive pointers
```go
var a = "hello world"
var Obj = &struct {
Foo *string
Bar *string
}{
Foo: &a,
Bar: &a,
}
dump.dump(Obj)
```
will output
```go
var p1Var = "hello world"
var p1 = &p1Var
var Obj = &struct { Foo *string; Bar *string }{
Foo: p1,
Bar: p1,
}
```
### Reused pointers
```go
type Zeo struct {
A *string
}
var z = Zeo {
A: "hello world",
}
var Obj = &struct {
Foo *Zeo
Bar *Zeo
}{
Foo: &z,
Bar: &z,
}
dump.dump(Obj)
```
will output
```go
var p1 = &Zeo{
A: "hello world",
}
var Obj = &struct { Foo *Zeo; Bar *Zeo }{
Foo: p1,
Bar: p1,
}
```