grepr

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2022 License: Unlicense Imports: 6 Imported by: 0

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 Default = Conf{Indent: gg.Indent}

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

Functions

func CanBackquote added in v0.0.4

func CanBackquote[A gg.Text](src A) bool

Corrected version of `strconv.CanBackquote` that allows newlines.

func Prn added in v0.0.5

func Prn(desc string, src any)

Shortcut for printing a given value as Go code, prefixed with a description. Very handy for debug-printing.

func String

func String(src any) string

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

Example
package main

import (
	"fmt"

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

type Inner struct {
	InnerId   *int
	InnerName *string
}

type Embed struct {
	EmbedId   int
	EmbedName string
}

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

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 StringIndent

func StringIndent(src any, lvl int) string

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

Types

type Conf

type Conf struct {
	Indent     string
	ZeroFields bool
}

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.

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".

func (*Fmt) Any

func (self *Fmt) Any(src any)

Formats the input into the inner buffer, using the inner config.

func (*Fmt) Value

func (self *Fmt) Value(src r.Value)

Formats the input into the inner buffer, using the inner config.

Jump to

Keyboard shortcuts

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