Skip to content

Commit

Permalink
fix: encoding of a zero-value duration in string format (#3)
Browse files Browse the repository at this point in the history
Instead of returning the constant byte slice containing the
representation of the zero duration, appends its content to
the destination slice.

resolve #2
  • Loading branch information
wI2L authored Nov 2, 2021
1 parent ec04aa1 commit b174f9b
Show file tree
Hide file tree
Showing 6 changed files with 7,844 additions and 7,699 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project are documented in this file.

**THIS LIBRARY IS STILL IN ALPHA AND THERE ARE NO GUARANTEES REGARDING API STABILITY YET**

## [v0.7.3] - 2021-11-02
- Fix the encoding of zero-value time.Duration type in string format.

## [v0.7.2] - 2021-08-31
- Minor performances improvements (remove inlined functions in `appendEscapedBytes`).

Expand Down Expand Up @@ -63,6 +66,7 @@ This includes the following changes, but not limited to:
## [v0.1.0] - 2019-08-30
Initial realease.

[v0.7.3]: https://github.com/wI2L/jettison/compare/v0.7.2...v0.7.3
[v0.7.2]: https://github.com/wI2L/jettison/compare/v0.7.1...v0.7.2
[v0.7.1]: https://github.com/wI2L/jettison/compare/v0.7.0...v0.7.1
[v0.7.0]: https://github.com/wI2L/jettison/compare/v0.6.0...v0.7.0
Expand Down
2 changes: 1 addition & 1 deletion time.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func appendDuration(dst []byte, d time.Duration) []byte {
l--
switch {
case u == 0:
return zeroDuration
return append(dst, zeroDuration...)
case u < uint64(time.Microsecond):
prec = 0
buf[l] = 'n'
Expand Down
16 changes: 16 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ func TestDurationFmtString(t *testing.T) {
}
}

func TestIssue2(t *testing.T) {
type x struct {
F time.Duration `json:"foobar" yaml:"foobar"`
}
xx := &x{}
b, err := MarshalOpts(xx, DurationFormat(DurationString))
if err != nil {
t.Error(err)
}
const want = `{"foobar":"0s"}`

if s := string(b); s != want {
t.Errorf("expected %q, got %q", want, s)
}
}

func TestAppendDuration(t *testing.T) {
// Taken from https://golang.org/src/time/time_test.go
var testdata = []struct {
Expand Down
26 changes: 13 additions & 13 deletions tools/charts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const path = require('path')
const prog = require('commander')
const util = require('util')
const deca = require('decamelize')
const SVGO = require('svgo')
const { optimize } = require('svgo');

prog
.option('-f, --input <file>', 'json-formatted benchmark data file')
Expand Down Expand Up @@ -67,22 +67,22 @@ function exportChartAsSVG (e, name) {
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg')
svg.setAttribute('version', '1.1')

var svgo = new SVGO({
const result = optimize(svg.outerHTML, {
plugins: [{
sortAttrs: true
name: "sortAttrs"
}, {
removeAttrs: {
attrs: '(clip-path|aria-label|overflow)'
name: "removeAttrs",
params: {
attrs: "(clip-path|aria-label|overflow)"
}
}]
})
svgo.optimize(svg.outerHTML, {}).then(function (result) {
try {
return fs.writeFileSync(filename, result.data)
} catch (err) {
console.error('cannot write svg chart %s: %s', filename, err)
}
}],
multipass: true
})
try {
return fs.writeFileSync(filename, result.data)
} catch (err) {
console.error('cannot write svg chart %s: %s', filename, err)
}
}

function htmlToElement (html) {
Expand Down
Loading

0 comments on commit b174f9b

Please sign in to comment.