Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
popematt committed Feb 1, 2023
1 parent 78ab5b6 commit ec09c68
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 123 deletions.
56 changes: 27 additions & 29 deletions ion/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,44 +42,42 @@ type Marshaler interface {
//
// Different Go types can be passed into MarshalText() to be marshalled to their corresponding Ion types. e.g.,
//
// val, err := MarshalText(9)
// if err != nil {
// t.Fatal(err)
// }
// fmt.Println(string(val)) // prints out: 9
// val, err := MarshalText(9)
// if err != nil {
// t.Fatal(err)
// }
// fmt.Println(string(val)) // prints out: 9
//
// type inner struct {
// B int `ion:"b"`
// }
// type root struct {
// A inner `ion:"a"`
// C int `ion:"c"`
// }
//
// v = root{A: inner{B: 6}, C: 7}
// val, err = MarshalText(v)
// if err != nil {
// t.Fatal(err)
// }
// fmt.Println(string(val)) // prints out: {a:{b:6},c:7}
// type inner struct {
// B int `ion:"b"`
// }
// type root struct {
// A inner `ion:"a"`
// C int `ion:"c"`
// }
//
// v = root{A: inner{B: 6}, C: 7}
// val, err = MarshalText(v)
// if err != nil {
// t.Fatal(err)
// }
// fmt.Println(string(val)) // prints out: {a:{b:6},c:7}
//
// Should the value for marshalling require annotations, it must be wrapped in a
// Go struct with exactly 2 fields, where the other field of the struct is a slice of
// string and tagged `ion:",annotations"`, and this field can carry all the desired
// annotations.
//
// type foo struct {
// Value int
// AnyName []string `ion:",annotations"`
// }
//
// v := foo{5, []string{"some", "annotations"}} //some::annotations::5
// val, err := MarshalText(v)
// if err != nil {
// t.Fatal(err)
// }
// type foo struct {
// Value int
// AnyName []string `ion:",annotations"`
// }
//
// v := foo{5, []string{"some", "annotations"}} //some::annotations::5
// val, err := MarshalText(v)
// if err != nil {
// t.Fatal(err)
// }
func MarshalText(v interface{}) ([]byte, error) {
buf := bytes.Buffer{}
w := NewTextWriterOpts(&buf, TextWriterQuietFinish)
Expand Down
39 changes: 19 additions & 20 deletions ion/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import (
// Next moves the Reader to the position after the final value in the stream, it returns
// false, making it easy to loop through the values in a stream.
//
// var r Reader
// for r.Next() {
// // ...
// }
// var r Reader
// for r.Next() {
// // ...
// }
//
// Next also returns false in case of error. This can be distinguished from a legitimate
// end-of-stream by calling Err after exiting the loop.
Expand All @@ -56,22 +56,21 @@ import (
// outer sequence of values. The Reader will be positioned at the end of the composite value,
// such that a call to Next will move to the immediately-following value (if any).
//
// r := NewTextReaderStr("[foo, bar] [baz]")
// for r.Next() {
// if err := r.StepIn(); err != nil {
// return err
// }
// for r.Next() {
// fmt.Println(r.StringValue())
// }
// if err := r.StepOut(); err != nil {
// return err
// }
// }
// if err := r.Err(); err != nil {
// return err
// }
//
// r := NewTextReaderStr("[foo, bar] [baz]")
// for r.Next() {
// if err := r.StepIn(); err != nil {
// return err
// }
// for r.Next() {
// fmt.Println(r.StringValue())
// }
// if err := r.StepOut(); err != nil {
// return err
// }
// }
// if err := r.Err(); err != nil {
// return err
// }
type Reader interface {
// Next advances the Reader to the next position in the current value stream.
// It returns true if this is the position of an Ion value, and false if it
Expand Down
18 changes: 8 additions & 10 deletions ion/skipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ func (t *tokenizer) SkipDot() (bool, error) {
return true, nil
}

// SkipLobWhitespace skips whitespace when we're inside a large
// object ({{ ///= }} or {{ '''///=''' }}) where comments are
// not allowed.
// SkipLobWhitespace skips whitespace when we're inside a blob
// or clob where comments are not allowed.
func (t *tokenizer) SkipLobWhitespace() (int, error) {
c, _, err := t.skipLobWhitespace()
return c, err
Expand Down Expand Up @@ -502,16 +501,16 @@ func (t *tokenizer) skipStringHelper() error {
}
}

// SkipLongString skips over a '''-enclosed string, returning the next
// character after the closing '''.
// SkipLongString skips over a triple-quote-enclosed string, returning the next
// character after the closing triple-quote.
func (t *tokenizer) skipLongString() (int, error) {
if err := t.skipLongStringHelper(t.skipCommentsHandler); err != nil {
return 0, err
}
return t.read()
}

// SkipLongStringHelper skips over a '''-enclosed string.
// SkipLongStringHelper skips over a triple-quote-enclosed string.
func (t *tokenizer) skipLongStringHelper(handler commentHandler) error {
for {
c, err := t.read()
Expand Down Expand Up @@ -544,7 +543,7 @@ func (t *tokenizer) skipLongStringHelper(handler commentHandler) error {
// of the long string, and if we have consumed any ' characters. Also, it can detect
// if another long string starts after the current one; in that case, it returns
// false indicating this is not the end of the long string, and true for consumed '
// as we have read the closing ''' of the first long string.
// as we have read the closing triple-quote of the first long string.
func (t *tokenizer) skipEndOfLongString(handler commentHandler) (bool, bool, error) {
isConsumed := false
// We just read a ', check for two more ''s.
Expand Down Expand Up @@ -756,9 +755,8 @@ func (t *tokenizer) skipWhitespaceHelper() (bool, error) {
return ok, err
}

// SkipLobWhitespace skips whitespace when we're inside a large
// object ({{ ///= }} or {{ '''///=''' }}) where comments are
// not allowed.
// SkipLobWhitespace skips whitespace when we're inside a blob
// or clob, where comments are not allowed.
func (t *tokenizer) skipLobWhitespace() (int, bool, error) {
// Comments are not allowed inside a lob value; if we see a '/',
// it's the start of a base64-encoded value.
Expand Down
4 changes: 3 additions & 1 deletion ion/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,9 @@ func (t *tokenizer) ReadLongClob() ([]byte, error) {
return val, nil
}

// IsTripleQuote returns true if this is a triple-quote sequence (''').
// IsTripleQuote returns true if this is a triple-quote sequence; i.e.:
//
// '''
func (t *tokenizer) IsTripleQuote() (bool, error) {
// We've just read a '\'', check if the next two are too.
cs, err := t.peekN(2)
Expand Down
76 changes: 37 additions & 39 deletions ion/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,55 +48,53 @@ type Unmarshaler interface {
// User must pass the proper object type to the unmarshalled Ion data.
// Below is the mapping between Go native type and Ion types. e.g.,
//
// boolBytes := []byte{0xE0, 0x01, 0x00, 0xEA, 0x11}
// var boolVal bool
// err := Unmarshal(boolBytes, &boolVal)
// if err != nil {
// t.Fatal(err)
// }
// fmt.Println(boolVal) // prints out: true
//
// err = UnmarshalString("true", &boolVal)
// if err != nil {
// t.Fatal(err)
// }
// fmt.Println(boolVal) // prints out: true
// boolBytes := []byte{0xE0, 0x01, 0x00, 0xEA, 0x11}
// var boolVal bool
// err := Unmarshal(boolBytes, &boolVal)
// if err != nil {
// t.Fatal(err)
// }
// fmt.Println(boolVal) // prints out: true
//
// err = UnmarshalString("true", &boolVal)
// if err != nil {
// t.Fatal(err)
// }
// fmt.Println(boolVal) // prints out: true
//
// To unmarshal an Ion value with annotations, the object passed to Unmarshal
// must be a Go struct with exactly two fields, where one field's type
// is in accordance with the Ion type which needs to be unmarshalled (list
// of mapping between Go native types and Ion types below); and the other
// field must be of type []string and tagged as `ion:",annotations"`.
//
// type foo struct {
// Value int // or interface{}
// AnyName []string `ion:",annotations"`
// }
//
// var val foo
// err := UnmarshalString("age::10", &val)
// if err != nil {
// t.Fatal(err)
// }
// fmt.Println(val) // prints out: {10 [age]}
// type foo struct {
// Value int // or interface{}
// AnyName []string `ion:",annotations"`
// }
//
// Go native type Ion Type
// -------------------------- ---------------
// nil/interface{} null
// bool/interface{} bool
// Any ints/uints/big.Int/interface{} int
// float32/float64/interface{} float
// ion.Decimal/interface{} decimal
// ion.Timestamp/interface{} timestamp
// string/interface{} symbol
// string/interface{} string
// []byte/[]interface{}{} clob
// []byte/[]interface{}{} blob
// []interface{}{} list
// []interface{}{} sexp
// map[string]interface{}{}/struct/interface{} struct
// var val foo
// err := UnmarshalString("age::10", &val)
// if err != nil {
// t.Fatal(err)
// }
// fmt.Println(val) // prints out: {10 [age]}
//
// Go native type Ion Type
// -------------------------- ---------------
// nil/interface{} null
// bool/interface{} bool
// Any ints/uints/big.Int/interface{} int
// float32/float64/interface{} float
// ion.Decimal/interface{} decimal
// ion.Timestamp/interface{} timestamp
// string/interface{} symbol
// string/interface{} string
// []byte/[]interface{}{} clob
// []byte/[]interface{}{} blob
// []interface{}{} list
// []interface{}{} sexp
// map[string]interface{}{}/struct/interface{} struct
func Unmarshal(data []byte, v interface{}, ssts ...SharedSymbolTable) error {
catalog := NewCatalog(ssts...)
return NewDecoder(NewReaderCat(bytes.NewReader(data), catalog)).DecodeTo(v)
Expand Down
47 changes: 23 additions & 24 deletions ion/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,41 @@ import (
// calls to Write will write values inside of the container until a matching
// End method is called.
//
// var w Writer
// w.BeginSexp()
// {
// w.WriteInt(1)
// w.WriteSymbolFromString("+")
// w.WriteInt(1)
// }
// w.EndSexp()
// var w Writer
// w.BeginSexp()
// {
// w.WriteInt(1)
// w.WriteSymbolFromString("+")
// w.WriteInt(1)
// }
// w.EndSexp()
//
// When writing values inside a struct, the FieldName method must be called before
// each value to set the value's field name. The Annotation method may likewise
// be called before writing any value to add an annotation to the value.
//
// var w Writer
// w.Annotation("user")
// w.BeginStruct()
// {
// w.FieldName("id")
// w.WriteString("foo")
// w.FieldName("name")
// w.WriteString("bar")
// }
// w.EndStruct()
// var w Writer
// w.Annotation("user")
// w.BeginStruct()
// {
// w.FieldName("id")
// w.WriteString("foo")
// w.FieldName("name")
// w.WriteString("bar")
// }
// w.EndStruct()
//
// When you're done writing values, you should call Finish to ensure everything has
// been flushed from in-memory buffers. While individual methods all return an error
// on failure, implementations will remember any errors, no-op subsequent calls, and
// return the previous error. This lets you keep code a bit cleaner by only checking
// the return value of the final method call (generally Finish).
//
// var w Writer
// writeSomeStuff(w)
// if err := w.Finish(); err != nil {
// return err
// }
//
// var w Writer
// writeSomeStuff(w)
// if err := w.Finish(); err != nil {
// return err
// }
type Writer interface {
// FieldName sets the field name for the next value written.
FieldName(val SymbolToken) error
Expand Down

0 comments on commit ec09c68

Please sign in to comment.