Skip to content

Instantly share code, notes, and snippets.

@bprosnitz
Created May 11, 2016 20:33
Show Gist options
  • Save bprosnitz/ea9ac0d3688c8b641a3dcb6f4f37b77c to your computer and use it in GitHub Desktop.
Save bprosnitz/ea9ac0d3688c8b641a3dcb6f4f37b77c to your computer and use it in GitHub Desktop.
A vdl encoder that prints debug messages (doesn't wrap)
package vdl
import (
"fmt"
"os"
"strings"
)
func encoderDebugMsg(method string, depth int, args ...interface{}) {
argStrs := make([]string, len(args))
for i := range args {
argStrs[i] = fmt.Sprintf("%v", args[i])
}
fmt.Fprintf(os.Stderr, "%s %s(%s)\n", strings.Repeat("\t", depth), method, strings.Join(argStrs, ", "))
}
type DebuggingEncoder struct {
depth int
}
func (t *DebuggingEncoder) msg(method string, args ...interface{}) {
encoderDebugMsg(method, t.depth, args...)
}
func (t *DebuggingEncoder) EncodeBool(src bool) error {
t.msg("EncodeBool", src)
return nil
}
func (t *DebuggingEncoder) EncodeUint(src uint64) error {
t.msg("EncodeUint", src)
return nil
}
func (t *DebuggingEncoder) EncodeInt(src int64) error {
t.msg("EncodeInt", src)
return nil
}
func (t *DebuggingEncoder) EncodeFloat(src float64) error {
t.msg("EncodeFloat", src)
return nil
}
func (t *DebuggingEncoder) EncodeComplex(src complex128) error {
t.msg("EncodeComplex", src)
return nil
}
func (t *DebuggingEncoder) EncodeBytes(src []byte) error {
t.msg("EncodeBytes", src)
return nil
}
func (t *DebuggingEncoder) EncodeString(src string) error {
t.msg("EncodeString", src)
return nil
}
func (t *DebuggingEncoder) EncodeTypeObject(tt *Type) error {
t.msg("EncodeTypeObject", tt)
return nil
}
func (t *DebuggingEncoder) StartValue(tt *Type) error {
t.msg("StartValue", tt)
return nil
}
func (t *DebuggingEncoder) FinishValue() error {
t.msg("FinishValue")
return nil
}
func (t *DebuggingEncoder) NilValue(tt *Type) error {
t.msg("NilValue", tt)
return nil
}
func (t *DebuggingEncoder) NextEntry(done bool) error {
t.msg("NextEntry")
return nil
}
func (t *DebuggingEncoder) NextField(name string) error {
t.msg("NextField", name)
return nil
}
func (t *DebuggingEncoder) SetLenHint(lenHint int) error {
t.msg("SetLenHint", lenHint)
return nil
}
func (t *DebuggingEncoder) SetNextStartValueIsOptional() {
t.msg("SetNextStartValueIsOptional")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment