Skip to content

Commit

Permalink
document: Google docs writes out incorrect floating point values
Browse files Browse the repository at this point in the history
Relax our parsing somewhat so we can read these documents,
truncating at the decimal point back to an integer.

Fixes unidoc#196
  • Loading branch information
tbaliance committed Sep 12, 2018
1 parent e6c0562 commit 75f877d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 18 deletions.
41 changes: 41 additions & 0 deletions common/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2018 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting [email protected].

package common_test

import (
"testing"

"baliance.com/gooxml/schema/soo/wml"
)

// Fields of these types must be integers per the spec, but Google doc
// writes out documents with floating point values. Relax our parsing
// somewhat so we can read these documents, truncating at the decimal
// point back to an integer.

// Issue #196

func TestParseGoogleDocsST_TwipsMeasure(t *testing.T) {
ms, err := wml.ParseUnionST_TwipsMeasure("123.4")
if err != nil {
t.Fatalf("expected no error, got %s", err)
}
if *ms.ST_UnsignedDecimalNumber != 123 {
t.Errorf("expected 123, got %#v", ms)
}
}

func TestParseGoogleDocsST_MeasurementOrPercent(t *testing.T) {
mp, err := wml.ParseUnionST_MeasurementOrPercent("123.4")
if err != nil {
t.Fatalf("expected no error, got %s", err)
}
if *mp.ST_DecimalNumberOrPercent.ST_UnqualifiedPercentage != 123 {
t.Errorf("expected 123, got %#v", mp)
}
}
4 changes: 2 additions & 2 deletions schema/soo/ofc/math/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ func ParseUnionST_TwipsMeasure(s string) (sharedTypes.ST_TwipsMeasure, error) {
if sharedTypes.ST_PositiveUniversalMeasurePatternRe.MatchString(s) {
ret.ST_PositiveUniversalMeasure = &s
} else {
v, err := strconv.ParseUint(s, 10, 64)
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return ret, fmt.Errorf("parsing %s as uint: %s", s, err)
}
ret.ST_UnsignedDecimalNumber = &v
ret.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(v))
}
return ret, nil
}
Expand Down
28 changes: 14 additions & 14 deletions schema/soo/wml/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func ParseUnionST_SignedTwipsMeasure(s string) (ST_SignedTwipsMeasure, error) {
if sharedTypes.ST_UniversalMeasurePatternRe.MatchString(s) {
r.ST_UniversalMeasure = &s
} else {
v, err := strconv.ParseInt(s, 10, 32)
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return r, fmt.Errorf("parsing %s as int: %s", s, err)
}
r.Int64 = &v
r.Int64 = gooxml.Int64(int64(v))
}
return r, nil
}
Expand All @@ -48,11 +48,11 @@ func ParseUnionST_TwipsMeasure(s string) (sharedTypes.ST_TwipsMeasure, error) {
if sharedTypes.ST_PositiveUniversalMeasurePatternRe.MatchString(s) {
ret.ST_PositiveUniversalMeasure = &s
} else {
v, err := strconv.ParseUint(s, 10, 64)
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return ret, fmt.Errorf("parsing %s as uint: %s", s, err)
}
ret.ST_UnsignedDecimalNumber = &v
ret.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(v))
}
return ret, nil
}
Expand Down Expand Up @@ -80,11 +80,11 @@ func ParseUnionST_DecimalNumberOrPercent(s string) (ST_DecimalNumberOrPercent, e
if sharedTypes.ST_PercentagePatternRe.MatchString(s) {
ret.ST_Percentage = &s
} else {
v, err := strconv.ParseInt(s, 10, 32)
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return ret, fmt.Errorf("parsing %s as int: %s", s, err)
}
ret.ST_UnqualifiedPercentage = &v
ret.ST_UnqualifiedPercentage = gooxml.Int64(int64(v))
}
return ret, nil
}
Expand All @@ -98,11 +98,11 @@ func ParseUnionST_MeasurementOrPercent(s string) (ST_MeasurementOrPercent, error
if sharedTypes.ST_PercentagePatternRe.MatchString(s) {
r.ST_DecimalNumberOrPercent.ST_Percentage = &s
} else {
v, err := strconv.ParseInt(s, 10, 32)
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return r, fmt.Errorf("parsing %s as int: %s", s, err)
}
r.ST_DecimalNumberOrPercent.ST_UnqualifiedPercentage = &v
r.ST_DecimalNumberOrPercent.ST_UnqualifiedPercentage = gooxml.Int64(int64(v))
}
}
return r, nil
Expand All @@ -114,11 +114,11 @@ func ParseUnionST_HpsMeasure(s string) (ST_HpsMeasure, error) {
if sharedTypes.ST_PositiveUniversalMeasurePatternRe.MatchString(s) {
r.ST_PositiveUniversalMeasure = &s
} else {
v, err := strconv.ParseUint(s, 10, 64)
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return r, fmt.Errorf("parsing %s as uint: %s", s, err)
}
r.ST_UnsignedDecimalNumber = &v
r.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(v))
}
return r, nil
}
Expand All @@ -128,11 +128,11 @@ func ParseUnionST_SignedHpsMeasure(s string) (ST_SignedHpsMeasure, error) {
if sharedTypes.ST_UniversalMeasurePatternRe.MatchString(s) {
r.ST_UniversalMeasure = &s
} else {
v, err := strconv.ParseInt(s, 10, 32)
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return r, fmt.Errorf("parsing %s as int: %s", s, err)
}
r.Int64 = &v
r.Int64 = gooxml.Int64(int64(v))
}
return r, nil
}
Expand All @@ -142,11 +142,11 @@ func ParseUnionST_TextScale(s string) (ST_TextScale, error) {
if ST_TextScalePercentPatternRe.MatchString(s) {
r.ST_TextScalePercent = &s
} else {
v, err := strconv.ParseInt(s, 10, 32)
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return r, fmt.Errorf("parsing %s as int: %s", s, err)
}
r.ST_TextScaleDecimal = &v
r.ST_TextScaleDecimal = gooxml.Int64(int64(v))
}
return r, nil
}
Expand Down
2 changes: 1 addition & 1 deletion update-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ package gooxml
import "time"
// Release is the last release version of the software.
var ReleaseVersion = "v0.5000"
var ReleaseVersion = "v0.8000"
// ReleaseDate is the release date of the source code for licensing purposes.
var ReleaseDate = time.Date(`date +%Y`,`date +%_m`,`date +%_d`,0,0,0,0,time.UTC)
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ import "time"
var ReleaseVersion = "v0.8000"

// ReleaseDate is the release date of the source code for licensing purposes.
var ReleaseDate = time.Date(2018, 7, 27, 0, 0, 0, 0, time.UTC)
var ReleaseDate = time.Date(2018, 9, 11, 0, 0, 0, 0, time.UTC)

0 comments on commit 75f877d

Please sign in to comment.