go-jsonnet/internal/dump
Jesse 2655afd2bd
memory align structs (#635)
feat: align most structs and add lint rule to enforce struct alignment
2022-10-19 06:19:15 +01:00
..
BUILD.bazel Extract AST processing to separate packages 2019-08-28 20:16:47 +01:00
dump_test.go fix order-dependent test 2020-04-02 21:59:03 +02:00
dump.go memory align structs (#635) 2022-10-19 06:19:15 +01:00
pointermap.go Extract AST processing to separate packages 2019-08-28 20:16:47 +01:00
README.md Extract AST processing to separate packages 2019-08-28 20:16:47 +01:00
utils.go Make golangci-lint happy 2020-03-05 14:54:27 +01: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,
}