Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions node/basicnode/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,39 @@ func TestMap(t *testing.T) {
func BenchmarkMapStrInt_3n_AssembleStandard(b *testing.B) {
tests.SpecBenchmarkMapStrInt_3n_AssembleStandard(b, basicnode.Prototype.Map)
}

func BenchmarkMapStrInt_3n_AssembleEntry(b *testing.B) {
tests.SpecBenchmarkMapStrInt_3n_AssembleEntry(b, basicnode.Prototype.Map)
}

func BenchmarkMapStrInt_3n_Iteration(b *testing.B) {
tests.SpecBenchmarkMapStrInt_3n_Iteration(b, basicnode.Prototype.Map)
}

func BenchmarkMapStrInt_25n_AssembleStandard(b *testing.B) {
tests.SpecBenchmarkMapStrInt_25n_AssembleStandard(b, basicnode.Prototype.Map)
}

func BenchmarkMapStrInt_25n_AssembleEntry(b *testing.B) {
tests.SpecBenchmarkMapStrInt_25n_AssembleEntry(b, basicnode.Prototype.Map)
}

func BenchmarkMapStrInt_25n_Iteration(b *testing.B) {
tests.SpecBenchmarkMapStrInt_25n_Iteration(b, basicnode.Prototype.Map)
}

func BenchmarkSpec_Marshal_Map3StrInt(b *testing.B) {
tests.BenchmarkSpec_Marshal_Map3StrInt(b, basicnode.Prototype.Map)
}

func BenchmarkSpec_Marshal_MapNStrMap3StrInt(b *testing.B) {
tests.BenchmarkSpec_Marshal_MapNStrMap3StrInt(b, basicnode.Prototype.Map)
}

func BenchmarkSpec_Unmarshal_Map3StrInt(b *testing.B) {
tests.BenchmarkSpec_Unmarshal_Map3StrInt(b, basicnode.Prototype.Map)
}

func BenchmarkSpec_Unmarshal_MapNStrMap3StrInt(b *testing.B) {
tests.BenchmarkSpec_Unmarshal_MapNStrMap3StrInt(b, basicnode.Prototype.Map)
}
Expand Down Expand Up @@ -123,9 +129,9 @@ func TestMapBuilder(t *testing.T) {
actual := printer.Sprint(mapNode)

expect := `map{
string{"cat"}: string{"meow"}
string{"dog"}: string{"bark"}
string{"eel"}: string{"zap"}
string{"cat"}:string{"meow"},
string{"dog"}:string{"bark"},
string{"eel"}:string{"zap"}
}`
qt.Check(t, expect, qt.Equals, actual)

Expand Down
134 changes: 70 additions & 64 deletions printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
// All printer configuration will be the default;
// links will be printed, and will not be traversed.
func Print(n datamodel.Node) {
Config{}.Print(n)
Config{Indentation: []byte{'\t'}}.Print(n)

Check warning on line 18 in printer/printer.go

View check run for this annotation

Codecov / codecov/patch

printer/printer.go#L18

Added line #L18 was not covered by tests
}

// Sprint returns a textual description of the node tree.
// All printer configuration will be the default;
// links will be printed, and will not be traversed.
func Sprint(n datamodel.Node) string {
return Config{}.Sprint(n)
return Config{Indentation: []byte{'\t'}}.Sprint(n)
}

// Fprint accepts an io.Writer to which a textual description of the node tree will be written.
// All printer configuration will be the default;
// links will be printed, and will not be traversed.
func Fprint(w io.Writer, n datamodel.Node) {
Config{}.Fprint(w, n)
Config{Indentation: []byte{'\t'}}.Fprint(w, n)

Check warning on line 32 in printer/printer.go

View check run for this annotation

Codecov / codecov/patch

printer/printer.go#L32

Added line #L32 was not covered by tests
}

// Print emits a textual description of the node tree straight to stdout.
Expand All @@ -50,7 +50,6 @@
// The configuration structure this method is attached to can be used to specified details for how the printout will be formatted.
func (cfg Config) Fprint(w io.Writer, n datamodel.Node) {
pr := printBuf{w, cfg}
pr.Config.init()
pr.doString(0, printState_normal, n)
}

Expand All @@ -61,23 +60,16 @@
Abbreviate bool

// If set, the indentation to use.
// If nil, it will be treated as a default "\t".
// If nil, no indentation will be used and newlines will be omitted. If set to an empty slice,
// newlines will be used but no indentation. Setting to `[]byte{'\t'}` is a common choice.
Indentation []byte

// Probably does exactly what you think it does.
// If set, the string to use for the start of each line.
StartingIndent []byte

// Set to true if you like verbosity, I guess.
// If false, strings will only have kind+type markings if they're typed.
//
// Not yet supported.
AlwaysMarkStrings bool

// Set to true if you want type info to be skipped for any type that's in the Prelude
// (e.g. instead of `string<String>{` seeing only `string{` is preferred, etc).
//
// Not yet supported.
ElidePreludeTypeInfo bool
// If set to true, scalar values will be omitted from the output. This is useful for representing
// the structure of a graph without the data.
OmitScalarValues bool

// Set to true if you want maps to use "complex"-style printouts:
// meaning they will print their keys on separate lines than their values,
Expand All @@ -93,12 +85,6 @@
UseMapComplexStyleOnType map[schema.TypeName]bool
}

func (cfg *Config) init() {
if cfg.Indentation == nil {
cfg.Indentation = []byte{'\t'}
}
}

// oneline decides if a value should be flatted into printing on a single,
// or if it's allowed to spread out over multiple lines.
// Note that this will not be asked if something outside of a value has already declared it's
Expand Down Expand Up @@ -163,6 +149,12 @@
}
}

func (z *printBuf) doNewline() {
if z.Config.StartingIndent != nil || z.Config.Indentation != nil {
z.wr.Write([]byte{'\n'})
}
}

const (
printState_normal uint8 = iota
printState_isKey // may sometimes entersen or stringify things harder.
Expand Down Expand Up @@ -221,9 +213,8 @@
// Also, because it's possible for structs to be keys in a map themselves, they potentially need oneline emission.
// Or, to customize emission in another direction if being a key in a map that's printing in "complex" mode.
// FUTURE: there should also probably be some way to configure instructions to use their representation form instead.
oneline :=
printState == printState_isCmplxValue ||
printState != printState_isCmplxKey && z.Config.oneline(tn.Type(), printState == printState_isKey)
oneline := printState == printState_isCmplxValue ||
printState != printState_isCmplxKey && z.Config.oneline(tn.Type(), printState == printState_isKey)
deepen := 1
if printState == printState_isCmplxKey {
deepen = 2
Expand All @@ -234,7 +225,7 @@
}
z.writeString("{")
if !oneline && n.Length() > 0 {
z.writeString("\n")
z.doNewline()
}
for itr := n.MapIterator(); !itr.Done(); {
k, v, _ := itr.Next()
Expand All @@ -243,14 +234,13 @@
}
fn, _ := k.AsString()
z.writeString(fn)
z.writeString(": ")
z.writeString(":")
z.doString(indentLevel+deepen, childState, v)
if oneline {
if !itr.Done() {
z.writeString(", ")
}
} else {
z.writeString("\n")
if !itr.Done() {
z.writeString(",")
}
if !oneline {
z.doNewline()
}
}
if !oneline {
Expand Down Expand Up @@ -295,7 +285,7 @@
}
z.writeString("{")
if n.Length() > 0 {
z.writeString("\n")
z.doNewline()
} else {
z.writeString("}")
return
Expand All @@ -306,20 +296,23 @@
z.doIndent(indentLevel + 1)
z.writeString("!! map iteration step yielded error: ")
z.writeString(err.Error())
z.writeString("\n")
z.doNewline()

Check warning on line 299 in printer/printer.go

View check run for this annotation

Codecov / codecov/patch

printer/printer.go#L299

Added line #L299 was not covered by tests
break
}
z.doString(indentLevel+1, childKeyState, k)
z.writeString(": ")
z.writeString(":")
z.doString(indentLevel+1, printState_isValue, v)
z.writeString("\n")
if !itr.Done() {
z.writeString(",")
}
z.doNewline()
}
z.doIndent(indentLevel)
z.writeString("}")
case datamodel.Kind_List:
z.writeString("{")
if n.Length() > 0 {
z.writeString("\n")
z.doNewline()
} else {
z.writeString("}")
return
Expand All @@ -330,14 +323,17 @@
z.doIndent(indentLevel + 1)
z.writeString("!! list iteration step yielded error: ")
z.writeString(err.Error())
z.writeString("\n")
z.doNewline()

Check warning on line 326 in printer/printer.go

View check run for this annotation

Codecov / codecov/patch

printer/printer.go#L326

Added line #L326 was not covered by tests
break
}
z.doIndent(indentLevel + 1)
z.writeString(strconv.FormatInt(idx, 10))
z.writeString(": ")
z.writeString(":")
z.doString(indentLevel+1, printState_isValue, v)
z.writeString("\n")
if !itr.Done() {
z.writeString(",")
}
z.doNewline()
}
z.doIndent(indentLevel)
z.writeString("}")
Expand All @@ -352,31 +348,41 @@
}
z.writeString("}")
case datamodel.Kind_Int:
x, _ := n.AsInt()
z.writeString("{")
z.writeString(strconv.FormatInt(x, 10))
z.writeString("}")
if !z.Config.OmitScalarValues {
x, _ := n.AsInt()
z.writeString("{")
z.writeString(strconv.FormatInt(x, 10))
z.writeString("}")
}
case datamodel.Kind_Float:
x, _ := n.AsFloat()
z.writeString("{")
z.writeString(strconv.FormatFloat(x, 'f', -1, 64))
z.writeString("}")
if !z.Config.OmitScalarValues {
x, _ := n.AsFloat()
z.writeString("{")
z.writeString(strconv.FormatFloat(x, 'f', -1, 64))
z.writeString("}")
}
case datamodel.Kind_String:
x, _ := n.AsString()
z.writeString("{")
z.writeString(strconv.QuoteToGraphic(x))
z.writeString("}")
if !z.Config.OmitScalarValues {
x, _ := n.AsString()
z.writeString("{")
z.writeString(strconv.QuoteToGraphic(x))
z.writeString("}")
}
case datamodel.Kind_Bytes:
x, _ := n.AsBytes()
z.writeString("{")
dst := make([]byte, hex.EncodedLen(len(x)))
hex.Encode(dst, x)
z.writeString(string(dst))
z.writeString("}")
if !z.Config.OmitScalarValues {
x, _ := n.AsBytes()
z.writeString("{")
dst := make([]byte, hex.EncodedLen(len(x)))
hex.Encode(dst, x)
z.writeString(string(dst))
z.writeString("}")
}
case datamodel.Kind_Link:
x, _ := n.AsLink()
z.writeString("{")
z.writeString(x.String())
z.writeString("}")
if !z.Config.OmitScalarValues {
x, _ := n.AsLink()
z.writeString("{")
z.writeString(x.String())
z.writeString("}")
}
}
}
Loading
Loading