dump

package
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

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,
}

Documentation

Overview

Package dump can dump a Go data structure to Go source file, so that it can be statically embedded into other code.

Index

Constants

This section is empty.

Variables

View Source
var Config = Options{
	StripPackageNames:   false,
	HidePrivateFields:   true,
	VariableName:        "Obj",
	VariableDescription: "",
}

Config is the default config used when calling Dump

Functions

func Dump

func Dump(value interface{})

Dump a value to stdout

func GetPointers

func GetPointers(v reflect.Value) ([]uintptr, []uintptr, []uintptr)

GetPointers recursively maps all pointers mentioned in a tree, breaking circular references, and provides a list of: * all pointers * all pointers that were referenced at least twice * all pointers that point to primitive type

func Sdump

func Sdump(value interface{}) string

Sdump dumps a value to a string

Types

type Options

type Options struct {
	HomePackage         string
	VariableName        string
	VariableDescription string
	StripPackageNames   bool
	HidePrivateFields   bool
}

Options represents configuration option

func (Options) Dump

func (o Options) Dump(value interface{})

Dump a value to stdout according to the options

func (Options) Sdump

func (o Options) Sdump(value interface{}) string

Sdump dumps a value to a string according to the options

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL