Skip to content

Commit

Permalink
A bit of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed May 9, 2021
1 parent c64dc8d commit 712dd17
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 458 deletions.
4 changes: 4 additions & 0 deletions notes
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

- use time format stuff in alt

- alt.Match
- matches only elements in the pattern/target fingerprint


- SEN
- use common writer?
- update like oj
Expand Down
10 changes: 5 additions & 5 deletions oj/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (wr *Writer) colorJSON(data interface{}, depth int) {

case time.Time:
wr.buf = append(wr.buf, wr.TimeColor...)
wr.appendTime(td)
wr.buf = wr.AppendTime(wr.buf, td, false)

case []interface{}:
wr.colorArray(td, depth)
Expand All @@ -79,15 +79,15 @@ func (wr *Writer) colorJSON(data interface{}, depth int) {
wr.colorObject(td, depth)

default:
if g, _ := data.(alt.Genericer); g != nil {
wr.colorJSON(g.Generic().Simplify(), depth)
return
}
if simp, _ := data.(alt.Simplifier); simp != nil {
data = simp.Simplify()
wr.colorJSON(data, depth)
return
}
if g, _ := data.(alt.Genericer); g != nil {
wr.colorJSON(g.Generic().Simplify(), depth)
return
}
if !wr.NoReflect {
if dec := alt.Decompose(data, &wr.Options); dec != nil {
wr.colorJSON(dec, depth)
Expand Down
8 changes: 4 additions & 4 deletions oj/tight.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (
)

func tightDefault(wr *Writer, data interface{}, _ int) {
if g, _ := data.(alt.Genericer); g != nil {
wr.appendJSON(g.Generic().Simplify(), 0)
return
}
if simp, _ := data.(alt.Simplifier); simp != nil {
data = simp.Simplify()
wr.appendJSON(data, 0)
return
}
if g, _ := data.(alt.Genericer); g != nil {
wr.appendJSON(g.Generic().Simplify(), 0)
return
}
if !wr.NoReflect {
rv := reflect.ValueOf(data)
kind := rv.Kind()
Expand Down
73 changes: 17 additions & 56 deletions oj/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ func (wr *Writer) appendJSON(data interface{}, depth int) {
wr.buf = wr.appendString(wr.buf, td, !wr.HTMLUnsafe)

case time.Time:
wr.appendTime(td)
wr.buf = wr.AppendTime(wr.buf, td, false)

case []interface{}:
// go marshal treats a nil slice as a special case different from an
// empty slice. Seems kind of odd but here is the check.
if wr.strict && td == nil {
wr.buf = append(wr.buf, []byte("null")...)
wr.buf = append(wr.buf, "null"...)
break
}
wr.appendArray(wr, td, depth)
Expand All @@ -207,15 +207,15 @@ func (wr *Writer) appendJSON(data interface{}, depth int) {
}

func appendDefault(wr *Writer, data interface{}, depth int) {
if g, _ := data.(alt.Genericer); g != nil {
wr.appendJSON(g.Generic().Simplify(), depth)
return
}
if simp, _ := data.(alt.Simplifier); simp != nil {
data = simp.Simplify()
wr.appendJSON(data, depth)
return
}
if g, _ := data.(alt.Genericer); g != nil {
wr.appendJSON(g.Generic().Simplify(), depth)
return
}
if !wr.NoReflect {
rv := reflect.ValueOf(data)
kind := rv.Kind()
Expand Down Expand Up @@ -244,45 +244,6 @@ func appendDefault(wr *Writer, data interface{}, depth int) {
}
}

func (wr *Writer) appendTime(t time.Time) {
if wr.TimeMap {
wr.buf = append(wr.buf, []byte(`{"`)...)
wr.buf = append(wr.buf, wr.CreateKey...)
wr.buf = append(wr.buf, []byte(`":"`)...)
if wr.FullTypePath {
wr.buf = append(wr.buf, []byte("time/Time")...)
} else {
wr.buf = append(wr.buf, []byte("Time")...)
}
wr.buf = append(wr.buf, []byte(`","value":`)...)
} else if 0 < len(wr.TimeWrap) {
wr.buf = append(wr.buf, []byte(`{"`)...)
wr.buf = append(wr.buf, []byte(wr.TimeWrap)...)
wr.buf = append(wr.buf, []byte(`":`)...)
}
switch wr.TimeFormat {
case "", "nano":
wr.buf = append(wr.buf, []byte(strconv.FormatInt(t.UnixNano(), 10))...)
case "second":
// Decimal format but float is not accurate enough so append the output
// in two parts.
nano := t.UnixNano()
secs := nano / int64(time.Second)
if 0 < nano {
wr.buf = append(wr.buf, []byte(fmt.Sprintf("%d.%09d", secs, nano-(secs*int64(time.Second))))...)
} else {
wr.buf = append(wr.buf, []byte(fmt.Sprintf("%d.%09d", secs, -(nano-(secs*int64(time.Second)))))...)
}
default:
wr.buf = append(wr.buf, '"')
wr.buf = append(wr.buf, []byte(t.Format(wr.TimeFormat))...)
wr.buf = append(wr.buf, '"')
}
if 0 < len(wr.TimeWrap) || wr.TimeMap {
wr.buf = append(wr.buf, '}')
}
}

func appendArray(wr *Writer, n []interface{}, depth int) {
var is string
var cs string
Expand Down Expand Up @@ -359,7 +320,7 @@ func appendObject(wr *Writer, n map[string]interface{}, depth int) {
continue
}
empty = false
wr.buf = append(wr.buf, []byte(cs)...)
wr.buf = append(wr.buf, cs...)
wr.buf = wr.appendString(wr.buf, k, !wr.HTMLUnsafe)
wr.buf = append(wr.buf, ':')
wr.buf = append(wr.buf, ' ')
Expand All @@ -368,7 +329,7 @@ func appendObject(wr *Writer, n map[string]interface{}, depth int) {
}
if !empty {
wr.buf[len(wr.buf)-1] = '\n'
wr.buf = append(wr.buf, []byte(is)...)
wr.buf = append(wr.buf, is...)
}
wr.buf = append(wr.buf, '}')
}
Expand Down Expand Up @@ -413,7 +374,7 @@ func appendSortObject(wr *Writer, n map[string]interface{}, depth int) {
continue
}
empty = false
wr.buf = append(wr.buf, []byte(cs)...)
wr.buf = append(wr.buf, cs...)
wr.buf = wr.appendString(wr.buf, k, !wr.HTMLUnsafe)
wr.buf = append(wr.buf, ':')
wr.buf = append(wr.buf, ' ')
Expand All @@ -422,7 +383,7 @@ func appendSortObject(wr *Writer, n map[string]interface{}, depth int) {
}
if !empty {
wr.buf[len(wr.buf)-1] = '\n'
wr.buf = append(wr.buf, []byte(is)...)
wr.buf = append(wr.buf, is...)
}
wr.buf = append(wr.buf, '}')
}
Expand Down Expand Up @@ -465,7 +426,7 @@ func (wr *Writer) appendStruct(rv reflect.Value, depth int, st *ojg.Struct) {
cs = spaces[0:x]
}
if 0 < len(wr.CreateKey) {
wr.buf = append(wr.buf, []byte(cs)...)
wr.buf = append(wr.buf, cs...)
wr.buf = append(wr.buf, '"')
wr.buf = append(wr.buf, wr.CreateKey...)
wr.buf = append(wr.buf, `": "`...)
Expand All @@ -479,7 +440,7 @@ func (wr *Writer) appendStruct(rv reflect.Value, depth int, st *ojg.Struct) {
}
for _, fi := range fields {
if !indented {
wr.buf = append(wr.buf, []byte(cs)...)
wr.buf = append(wr.buf, cs...)
indented = true
}
wr.buf, v, wrote, has = fi.Append(fi, wr.buf, rv, !wr.HTMLUnsafe)
Expand Down Expand Up @@ -527,7 +488,7 @@ func (wr *Writer) appendStruct(rv reflect.Value, depth int, st *ojg.Struct) {
}
if !empty {
wr.buf[len(wr.buf)-1] = '\n'
wr.buf = append(wr.buf, []byte(is)...)
wr.buf = append(wr.buf, is...)
}
wr.buf = append(wr.buf, '}')
}
Expand Down Expand Up @@ -563,7 +524,7 @@ func (wr *Writer) appendSlice(rv reflect.Value, depth int, st *ojg.Struct) {
empty := true
wr.buf = append(wr.buf, '[')
for j := 0; j < end; j++ {
wr.buf = append(wr.buf, []byte(cs)...)
wr.buf = append(wr.buf, cs...)
rm := rv.Index(j)
switch rm.Kind() {
case reflect.Struct:
Expand All @@ -580,7 +541,7 @@ func (wr *Writer) appendSlice(rv reflect.Value, depth int, st *ojg.Struct) {
}
if !empty {
wr.buf[len(wr.buf)-1] = '\n'
wr.buf = append(wr.buf, []byte(is)...)
wr.buf = append(wr.buf, is...)
}
wr.buf = append(wr.buf, ']')
}
Expand Down Expand Up @@ -626,7 +587,7 @@ func (wr *Writer) appendMap(rv reflect.Value, depth int, st *ojg.Struct) {
}
rm = rm.Elem()
}
wr.buf = append(wr.buf, []byte(cs)...)
wr.buf = append(wr.buf, cs...)
switch rm.Kind() {
case reflect.Struct:
wr.buf = wr.appendString(wr.buf, kv.String(), !wr.HTMLUnsafe)
Expand Down Expand Up @@ -656,7 +617,7 @@ func (wr *Writer) appendMap(rv reflect.Value, depth int, st *ojg.Struct) {
}
if !empty {
wr.buf[len(wr.buf)-1] = '\n'
wr.buf = append(wr.buf, []byte(is)...)
wr.buf = append(wr.buf, is...)
}
wr.buf = append(wr.buf, '}')
}
37 changes: 22 additions & 15 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,39 +206,46 @@ type Options struct {
Converter *Converter
}

// BuildTime appends a time string to the buffer.
func (o *Options) BuildTime(buf []byte, t time.Time) []byte {
// AppendTime appends a time string to the buffer.
func (o *Options) AppendTime(buf []byte, t time.Time, sen bool) []byte {
if o.TimeMap {
buf = append(buf, []byte(`{"`)...)
buf = append(buf, `{"`...)
buf = append(buf, o.CreateKey...)
buf = append(buf, []byte(`":`)...)
if o.FullTypePath {
buf = append(buf, []byte(`"time/Time"`)...)
buf = append(buf, `":`...)
if sen {
if o.FullTypePath {
buf = append(buf, `"time/Time" value:`...)
} else {
buf = append(buf, "Time value:"...)
}
} else {
buf = append(buf, []byte("Time")...)
if o.FullTypePath {
buf = append(buf, `"time/Time","value":`...)
} else {
buf = append(buf, `"Time","value":`...)
}
}
buf = append(buf, []byte(` value:`)...)
} else if 0 < len(o.TimeWrap) {
buf = append(buf, []byte(`{"`)...)
buf = append(buf, []byte(o.TimeWrap)...)
buf = append(buf, []byte(`":`)...)
buf = append(buf, `{"`...)
buf = append(buf, o.TimeWrap...)
buf = append(buf, `":`...)
}
switch o.TimeFormat {
case "", "nano":
buf = append(buf, []byte(strconv.FormatInt(t.UnixNano(), 10))...)
buf = strconv.AppendInt(buf, t.UnixNano(), 10)
case "second":
// Decimal format but float is not accurate enough so build the output
// in two parts.
nano := t.UnixNano()
secs := nano / int64(time.Second)
if 0 < nano {
buf = append(buf, []byte(fmt.Sprintf("%d.%09d", secs, nano-(secs*int64(time.Second))))...)
buf = append(buf, fmt.Sprintf("%d.%09d", secs, nano-(secs*int64(time.Second)))...)
} else {
buf = append(buf, []byte(fmt.Sprintf("%d.%09d", secs, -(nano-(secs*int64(time.Second)))))...)
buf = append(buf, fmt.Sprintf("%d.%09d", secs, -(nano-(secs*int64(time.Second))))...)
}
default:
buf = append(buf, '"')
buf = append(buf, []byte(t.Format(o.TimeFormat))...)
buf = t.AppendFormat(buf, o.TimeFormat)
buf = append(buf, '"')
}
if 0 < len(o.TimeWrap) || o.TimeMap {
Expand Down
2 changes: 1 addition & 1 deletion pretty/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (w *Writer) buildStringNode(v string) (n *node) {

func (w *Writer) buildTimeNode(v time.Time) (n *node) {
w.buf = w.buf[:0]
w.buf = w.BuildTime(w.buf, v)
w.buf = w.AppendTime(w.buf, v, w.SEN)
n = &node{size: len(w.buf), kind: strNode}
n.buf = make([]byte, len(w.buf))
copy(n.buf, w.buf)
Expand Down
Loading

0 comments on commit 712dd17

Please sign in to comment.