go-jsonnet/dump
Roger Peppe fde815f6a1 godoc.org-compatible ast package
Change the dump code so that it hides the values of variable
definitions if they're large. This means that godoc.org should
be able to deal with the output, and the godoc output is
readable without needing to read through a huge struct literal definition.

Other approaches might be to always generate an extra variable
(seems unnecessary) or to pass to writer explicitly to the dump
methods rather than swapping s.w out temporarily. The former
seems unnecessarily intrusive to the usual output; the latter
seemed unnecessarily intrusive to the source itself.
YMMV.
2018-04-12 14:06:52 -04:00
..
dump_test.go godoc.org-compatible ast package 2018-04-12 14:06:52 -04:00
dump.go godoc.org-compatible ast package 2018-04-12 14:06:52 -04:00
pointermap.go Apply more golint recommendations (#201) 2018-02-28 01:11:18 -05:00
README.md experiment dump ast to source code (#101) 2017-11-07 23:31:11 -05:00
utils.go Fix unparsing of enums, avoid static type error 2017-11-08 11:28:38 -05:00

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

var a = "hello world"

var Obj = &struct {
    Foo *string
    Bar *string
}{
	Foo: &a,
	Bar: &a,
}

dump.dump(Obj)

will output

var p1Var = "hello world"
var p1 = &p1Var
var Obj = &struct { Foo *string; Bar *string }{
	Foo: p1,
	Bar: p1,
}

Reused pointers

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

var p1 = &Zeo{
	A: "hello world",
}
var Obj = &struct { Foo *Zeo; Bar *Zeo }{
	Foo: p1,
	Bar: p1,
}