repr

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: MIT Imports: 9 Imported by: 130

README

Python's repr() for Go CircleCI

This package attempts to represent Go values in a form that can be used almost directly in Go source code.

Unfortunately, some values (such as pointers to basic types) can not be represented directly in Go. These values will be represented as &<value>. eg. &23

Example

type test struct {
  S string
  I int
  A []int
}

func main() {
  repr.Print(&test{
    S: "String",
    I: 123,
    A: []int{1, 2, 3},
  })
}

Outputs

&main.test{S: "String", I: 123, A: []int{1, 2, 3}}

Why repr and not pp?

pp is designed for printing coloured output to consoles, with (seemingly?) no way to disable this. If you don't want coloured output (eg. for use in diffs, logs, etc.) repr is for you.

Why repr and not go-spew?

Repr deliberately contains much less metadata about values. It is designed to (generally) be copyable directly into source code.

Compare go-spew:

(parser.expression) (len=1 cap=1) {
 (parser.alternative) (len=1 cap=1) {
  ([]interface {}) (len=1 cap=1) {
   (*parser.repitition)(0xc82000b220)({
    expression: (parser.expression) (len=2 cap=2) {
     (parser.alternative) (len=1 cap=1) {
      ([]interface {}) (len=1 cap=1) {
       (parser.str) (len=1) "a"
      }
     },
     (parser.alternative) (len=1 cap=1) {
      ([]interface {}) (len=1 cap=1) {
       (*parser.self)(0x593ef0)({
       })
      }
     }
    }
   })
  }
 }
}

To repr:

parser.expression{
  parser.alternative{
    []interface {}{
      &parser.repitition{
        expression: parser.expression{
          parser.alternative{
            []interface {}{
              parser.str("a"),
            },
          },
          parser.alternative{
            []interface {}{
              &parser.self{              },
            },
          },
        },
      },
    },
  },
}

Documentation

Overview

Package repr attempts to represent Go values in a form that can be copy-and-pasted into source code directly.

Some values (such as pointers to basic types) can not be represented directly in Go. These values will be output as `&<value>`. eg. `&23`

Index

Constants

This section is empty.

Variables

View Source
var Default = New(os.Stdout, Indent("  "))

Default prints to os.Stdout with two space indentation.

Functions

func Print

func Print(vs ...any)

Print writes a representation of v to os.Stdout, separated by spaces.

func Println

func Println(vs ...any)

Println prints v to os.Stdout, one per line.

func String

func String(v any, options ...Option) string

String returns a string representing v.

Types

type Option

type Option func(o *Printer)

An Option modifies the default behaviour of a Printer.

func AlwaysIncludeType

func AlwaysIncludeType() Option

AlwaysIncludeType always includes explicit type information for each item.

func ExplicitTypes

func ExplicitTypes(ok bool) Option

ExplicitTypes adds explicit typing to slice and map struct values that would normally be inferred by Go.

func Hide

func Hide[T any]() Option

Hide excludes fields of the given type from representation.

func IgnoreGoStringer

func IgnoreGoStringer() Option

IgnoreGoStringer disables use of the .GoString() method.

func IgnorePrivate added in v0.3.0

func IgnorePrivate() Option

IgnorePrivate disables private field members from output.

func Indent

func Indent(indent string) Option

Indent output by this much.

func NoIndent

func NoIndent() Option

NoIndent disables indenting.

func OmitEmpty

func OmitEmpty(omitEmpty bool) Option

OmitEmpty sets whether empty field members should be omitted from output.

func ScalarLiterals added in v0.3.0

func ScalarLiterals() Option

ScalarLiterals forces the use of literals for scalars, rather than a string representation if available.

For example, `time.Hour` will be printed as `time.Duration(3600000000000)` rather than `time.Duration(1h0m0s)`.

type Printer

type Printer struct {
	// contains filtered or unexported fields
}

Printer represents structs in a printable manner.

func New

func New(w io.Writer, options ...Option) *Printer

New creates a new Printer on w with the given Options.

func (*Printer) Print

func (p *Printer) Print(vs ...any)

Print the values.

func (*Printer) Println

func (p *Printer) Println(vs ...any)

Println prints each value on a new line.

Jump to

Keyboard shortcuts

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