grepr

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2024 License: Unlicense Imports: 10 Imported by: 0

README

Missing feature of the standard library: printing arbitrary inputs as Go code, with proper spacing and support for multi-line output with indentation. The name "repr" stands for "representation" and alludes to the Python function with the same name.

API doc: https://pkg.go.dev/github.com/mitranim/gg/grepr

Example:

package mock

import (
  "fmt"

  "github.com/mitranim/gg"
  "github.com/mitranim/gg/grepr"
)

type Outer struct {
  OuterId   int
  OuterName string
  Embed
  Inner *Inner
}

type Embed struct {
  EmbedId   int
  EmbedName string
}

type Inner struct {
  InnerId   *int
  InnerName *string
}

func main() {
  fmt.Println(grepr.String(Outer{
    OuterName: `outer`,
    Embed:     Embed{EmbedId: 20},
    Inner:     &Inner{
      InnerId:   gg.Ptr(30),
      InnerName: gg.Ptr(`inner`),
    },
  }))

  /**
  mock.Outer{
    OuterName: `outer`,
    Embed: mock.Embed{EmbedId: 20},
    Inner: &mock.Inner{
      InnerId: gg.Ptr(30),
      InnerName: gg.Ptr(`inner`),
    },
  }
  */
}

Documentation

Overview

Missing feature of the standard library: printing arbitrary inputs as Go code, with proper spacing and support for multi-line output with indentation. The name "repr" stands for "representation" and alludes to the Python function with the same name.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ConfDefault = Conf{Indent: gg.Indent}

Default config used by top-level formatting functions in this package.

View Source
var ConfFull = Conf{Indent: gg.Indent, ZeroFields: true}

Config that allows formatting of struct zero fields.

Functions

func Bytes added in v0.1.7

func Bytes[A any](src A) []byte

Similar to `fmt.Sprintf("%#v")` or `gg.GoString`, but more advanced. Formats the input as Go code, using the config `ConfDefault`, returning the resulting bytes.

func BytesC added in v0.1.7

func BytesC[A any](conf Conf, src A) []byte

Formats the input as Go code, using the given config, returning the resulting bytes.

func BytesIndent added in v0.1.7

func BytesIndent[A any](src A, lvl int) []byte

Formats the input as Go code, using the default config with the given indentation level, returning the resulting bytes.

func BytesIndentC added in v0.1.7

func BytesIndentC[A any](conf Conf, src A, lvl int) []byte

Formats the input as Go code, using the given config with the given indentation level, returning the resulting bytes.

func Println added in v0.1.0

func Println[A any](src A)

Shortcut for printing the input as Go code, using the default config.

func PrintlnC added in v0.1.7

func PrintlnC[A any](conf Conf, src A)

Shortcut for printing the input as Go code, using the given config.

func Prn added in v0.0.5

func Prn[A any](desc string, src A)

Shortcut for printing the input as Go code, prefixed with the given description, using the default config. Handy for debug-printing.

func String

func String[A any](src A) string

Similar to `fmt.Sprintf("%#v")` or `gg.GoString`, but more advanced. Formats the input as Go code, using the config `ConfDefault`, returning the resulting string.

Example
package main

import (
	"fmt"

	"github.com/mitranim/gg"
	"github.com/mitranim/gg/grepr"
)

type Outer struct {
	OuterId   int
	OuterName string
	Embed
	Inner *Inner
}

type Embed struct {
	EmbedId   int
	EmbedName string
}

type Inner struct {
	InnerId   *int
	InnerName *string
}

var testInner = Inner{
	InnerId:   gg.Ptr(30),
	InnerName: gg.Ptr(`inner`),
}

var testEmbed = Embed{EmbedId: 20}

var testOuter = Outer{
	OuterName: `outer`,
	Embed:     testEmbed,
	Inner:     &testInner,
}

func main() {
	fmt.Println(grepr.String(testOuter))
}
Output:

grepr_test.Outer{
    OuterName: `outer`,
    Embed: grepr_test.Embed{EmbedId: 20},
    Inner: &grepr_test.Inner{
        InnerId: gg.Ptr(30),
        InnerName: gg.Ptr(`inner`),
    },
}

func StringC added in v0.1.7

func StringC[A any](conf Conf, src A) string

Formats the input as Go code, using the given config, returning the resulting string.

func StringIndent

func StringIndent[A any](src A, lvl int) string

Formats the input as Go code, using the default config with the given indentation level, returning the resulting string.

func StringIndentC added in v0.1.7

func StringIndentC[A any](conf Conf, src A, lvl int) string

Formats the input as Go code, using the given config with the given indentation level, returning the resulting string.

Types

type Conf

type Conf struct {
	Indent     string
	ZeroFields bool
	Pkg        string
}

Formatting config.

  • `.Indent` controls indentation. If empty, output is single line.
  • `.ZeroFields`, if set, forces printing of zero fields in structs. By default zero fields are skipped.
  • `.Pkg`, if set, indicates the package name to strip from type names.

func (Conf) Fmt added in v0.1.0

func (self Conf) Fmt() Fmt

Shortcut for creating a pretty-formatter with this config.

func (Conf) IsMulti

func (self Conf) IsMulti() bool

Short for "is multi line". Inverse of `.IsSingle`.

func (Conf) IsSingle

func (self Conf) IsSingle() bool

Short for "is single line". If `.Indent` is empty, this is true, and output is single-line. Otherwise output is multi-line.

func (Conf) SkipZeroFields

func (self Conf) SkipZeroFields() bool

Inverse of `.ZeroFields`.

type Fmt

type Fmt struct {
	Conf
	gg.Buf
	Lvl       int
	ElideType bool
	Visited   gg.Set[u.Pointer]
}

Short for "formatter".

Jump to

Keyboard shortcuts

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