diff --git a/.github/workflows/pull.yml b/.github/workflows/pull.yml index c3debcb..9f1cddb 100644 --- a/.github/workflows/pull.yml +++ b/.github/workflows/pull.yml @@ -33,3 +33,12 @@ jobs: # Optional: if set to true then the action don't cache or restore ~/.cache/go-build. # skip-build-cache: true + + - name: Get dependencies + run: go mod tidy + + - name: Build + run: go build -v ./... + + - name: Test + run: go test $(go list ./...) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index e30c986..6324654 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -31,3 +31,12 @@ jobs: if: ${{ !github.head_ref }} continue-on-error: true uses: peter-evans/create-pull-request@v4 + + - name: Get dependencies + run: go mod tidy + + - name: Build + run: go build -v ./... + + - name: Test + run: go test $(go list ./...) diff --git a/helper.go b/helper.go index a3dd925..d91ad81 100644 --- a/helper.go +++ b/helper.go @@ -21,6 +21,8 @@ package docx import ( + "fmt" + "strconv" "unsafe" ) @@ -38,3 +40,31 @@ func StringToBytes(s string) (b []byte) { bh.cap = sh.len return b } + +// GetInt64 from string +func GetInt64(s string) (int64, error) { + v, err := strconv.ParseInt(s, 10, 64) + if err == nil { + return v, nil + } + v2, err := strconv.ParseFloat(s, 64) + if err == nil { + return int64(v2), nil + } + _, err = fmt.Sscanf(s, "%d", &v) + return v, err +} + +// GetInt from string +func GetInt(s string) (int, error) { + v, err := strconv.Atoi(s) + if err == nil { + return v, nil + } + v2, err := strconv.ParseFloat(s, 64) + if err == nil { + return int(v2), nil + } + _, err = fmt.Sscanf(s, "%d", &v) + return v, err +} diff --git a/structdrawing.go b/structdrawing.go index 76d2d7b..733bc8e 100644 --- a/structdrawing.go +++ b/structdrawing.go @@ -137,28 +137,19 @@ func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err for _, attr := range start.Attr { switch attr.Name.Local { case "distT": - r.DistT, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return - } + r.DistT, err = GetInt64(attr.Value) case "distB": - r.DistB, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return - } + r.DistB, err = GetInt64(attr.Value) case "distL": - r.DistL, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return - } + r.DistL, err = GetInt64(attr.Value) case "distR": - r.DistR, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return - } + r.DistR, err = GetInt64(attr.Value) default: // ignore other attributes } + if err != nil { + return err + } } for { t, err := d.Token() @@ -176,9 +167,9 @@ func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err for _, v := range tt.Attr { switch v.Name.Local { case "cx": - r.Extent.CX, err = strconv.ParseInt(v.Value, 10, 64) + r.Extent.CX, err = GetInt64(v.Value) case "cy": - r.Extent.CY, err = strconv.ParseInt(v.Value, 10, 64) + r.Extent.CY, err = GetInt64(v.Value) } if err != nil { return err @@ -189,13 +180,13 @@ func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err for _, v := range tt.Attr { switch v.Name.Local { case "l": - r.EffectExtent.L, err = strconv.ParseInt(v.Value, 10, 64) + r.EffectExtent.L, err = GetInt64(v.Value) case "t": - r.EffectExtent.T, err = strconv.ParseInt(v.Value, 10, 64) + r.EffectExtent.T, err = GetInt64(v.Value) case "r": - r.EffectExtent.R, err = strconv.ParseInt(v.Value, 10, 64) + r.EffectExtent.R, err = GetInt64(v.Value) case "b": - r.EffectExtent.B, err = strconv.ParseInt(v.Value, 10, 64) + r.EffectExtent.B, err = GetInt64(v.Value) } if err != nil { return err @@ -365,15 +356,12 @@ func (r *WPExtent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { for _, attr := range start.Attr { switch attr.Name.Local { case "cx": - r.CX, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return err - } + r.CX, err = GetInt64(attr.Value) case "cy": - r.CY, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return err - } + r.CY, err = GetInt64(attr.Value) + } + if err != nil { + return err } } // Consume the end element @@ -396,25 +384,16 @@ func (r *WPEffectExtent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) er for _, attr := range start.Attr { switch attr.Name.Local { case "l": - r.L, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return err - } + r.L, err = GetInt64(attr.Value) case "t": - r.T, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return err - } + r.T, err = GetInt64(attr.Value) case "r": - r.R, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return err - } + r.R, err = GetInt64(attr.Value) case "b": - r.B, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return err - } + r.B, err = GetInt64(attr.Value) + } + if err != nil { + return err } } // Consume the end element @@ -434,7 +413,7 @@ func (r *WPDocPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { for _, attr := range start.Attr { switch attr.Name.Local { case "id": - id, err := strconv.Atoi(attr.Value) + id, err := GetInt(attr.Value) if err != nil { return err } @@ -475,7 +454,7 @@ func (w *WPCNvGraphicFramePr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e if v == "" { continue } - w.Locks.NoChangeAspect, err = strconv.Atoi(v) + w.Locks.NoChangeAspect, err = GetInt(v) if err != nil { return err } @@ -706,7 +685,7 @@ func (p *PICNonVisualPicProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElem if v == "" { continue } - p.NonVisualDrawingProperties.ID, err = strconv.Atoi(v) + p.NonVisualDrawingProperties.ID, err = GetInt(v) if err != nil { return err } @@ -755,7 +734,7 @@ func (p *PicCNvPicPr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error { if v == "" { continue } - value.NoChangeAspect, err = strconv.Atoi(v) + value.NoChangeAspect, err = GetInt(v) if err != nil { return err } @@ -857,7 +836,7 @@ func (a *ABlip) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { if v == "" { continue } - value.Amount, err = strconv.Atoi(v) + value.Amount, err = GetInt(v) if err != nil { return err } @@ -990,23 +969,17 @@ func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) for _, attr := range start.Attr { switch attr.Name.Local { case "rot": - a.Rot, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return err - } + a.Rot, err = GetInt64(attr.Value) case "flipH": - a.FlipH, err = strconv.Atoi(attr.Value) - if err != nil { - return err - } + a.FlipH, err = GetInt(attr.Value) case "flipV": - a.FlipV, err = strconv.Atoi(attr.Value) - if err != nil { - return err - } + a.FlipV, err = GetInt(attr.Value) default: // ignore other attributes } + if err != nil { + return err + } } for { t, err := d.Token() @@ -1023,9 +996,9 @@ func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) for _, v := range tt.Attr { switch v.Name.Local { case "x": - a.Off.X, err = strconv.ParseInt(v.Value, 10, 64) + a.Off.X, err = GetInt64(v.Value) case "y": - a.Off.Y, err = strconv.ParseInt(v.Value, 10, 64) + a.Off.Y, err = GetInt64(v.Value) } if err != nil { return err @@ -1035,9 +1008,9 @@ func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) for _, v := range tt.Attr { switch v.Name.Local { case "cx": - a.Ext.CX, err = strconv.ParseInt(v.Value, 10, 64) + a.Ext.CX, err = GetInt64(v.Value) case "cy": - a.Ext.CY, err = strconv.ParseInt(v.Value, 10, 64) + a.Ext.CY, err = GetInt64(v.Value) } if err != nil { return err @@ -1048,9 +1021,9 @@ func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) for _, v := range tt.Attr { switch v.Name.Local { case "x": - value.X, err = strconv.ParseInt(v.Value, 10, 64) + value.X, err = GetInt64(v.Value) case "y": - value.Y, err = strconv.ParseInt(v.Value, 10, 64) + value.Y, err = GetInt64(v.Value) } if err != nil { return err @@ -1062,9 +1035,9 @@ func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) for _, v := range tt.Attr { switch v.Name.Local { case "cx": - value.CX, err = strconv.ParseInt(v.Value, 10, 64) + value.CX, err = GetInt64(v.Value) case "cy": - value.CY, err = strconv.ParseInt(v.Value, 10, 64) + value.CY, err = GetInt64(v.Value) } if err != nil { return err @@ -1228,55 +1201,28 @@ func (r *WPAnchor) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err for _, tt := range start.Attr { switch tt.Name.Local { case "distT": - r.DistT, err = strconv.ParseInt(tt.Value, 10, 64) - if err != nil { - return err - } + r.DistT, err = GetInt64(tt.Value) case "distB": - r.DistB, err = strconv.ParseInt(tt.Value, 10, 64) - if err != nil { - return err - } + r.DistB, err = GetInt64(tt.Value) case "distL": - r.DistL, err = strconv.ParseInt(tt.Value, 10, 64) - if err != nil { - return err - } + r.DistL, err = GetInt64(tt.Value) case "distR": - r.DistR, err = strconv.ParseInt(tt.Value, 10, 64) - if err != nil { - return err - } + r.DistR, err = GetInt64(tt.Value) case "simplePos": - r.SimplePos, err = strconv.Atoi(tt.Value) - if err != nil { - return err - } + r.SimplePos, err = GetInt(tt.Value) case "relativeHeight": - r.RelativeHeight, err = strconv.Atoi(tt.Value) - if err != nil { - return err - } + r.RelativeHeight, err = GetInt(tt.Value) case "behindDoc": - r.BehindDoc, err = strconv.Atoi(tt.Value) - if err != nil { - return err - } + r.BehindDoc, err = GetInt(tt.Value) case "locked": - r.Locked, err = strconv.Atoi(tt.Value) - if err != nil { - return err - } + r.Locked, err = GetInt(tt.Value) case "layoutInCell": - r.LayoutInCell, err = strconv.Atoi(tt.Value) - if err != nil { - return err - } + r.LayoutInCell, err = GetInt(tt.Value) case "allowOverlap": - r.AllowOverlap, err = strconv.Atoi(tt.Value) - if err != nil { - return err - } + r.AllowOverlap, err = GetInt(tt.Value) + } + if err != nil { + return err } } for { @@ -1295,9 +1241,9 @@ func (r *WPAnchor) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err for _, v := range tt.Attr { switch v.Name.Local { case "x": - r.SimplePosXY.X, err = strconv.ParseInt(v.Value, 10, 64) + r.SimplePosXY.X, err = GetInt64(v.Value) case "y": - r.SimplePosXY.Y, err = strconv.ParseInt(v.Value, 10, 64) + r.SimplePosXY.Y, err = GetInt64(v.Value) } if err != nil { return err diff --git a/structeffects.go b/structeffects.go index 5aa4741..bfe85aa 100644 --- a/structeffects.go +++ b/structeffects.go @@ -23,7 +23,6 @@ package docx import ( "encoding/xml" "io" - "strconv" "strings" ) @@ -263,7 +262,7 @@ func (r *NonVisualProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElemen for _, attr := range start.Attr { switch attr.Name.Local { case "id": - r.ID, err = strconv.Atoi(attr.Value) + r.ID, err = GetInt(attr.Value) if err != nil { return } @@ -295,22 +294,22 @@ func (s *Spacing) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err erro for _, attr := range start.Attr { switch attr.Name.Local { case "val": - s.Val, err = strconv.Atoi(attr.Value) + s.Val, err = GetInt(attr.Value) if err != nil { return } case "beforeLines": - s.BeforeLines, err = strconv.Atoi(attr.Value) + s.BeforeLines, err = GetInt(attr.Value) if err != nil { return } case "before": - s.Before, err = strconv.Atoi(attr.Value) + s.Before, err = GetInt(attr.Value) if err != nil { return } case "line": - s.Line, err = strconv.Atoi(attr.Value) + s.Line, err = GetInt(attr.Value) if err != nil { return } @@ -345,7 +344,7 @@ func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) { if attr.Value == "" { continue } - i.LeftChars, err = strconv.Atoi(attr.Value) + i.LeftChars, err = GetInt(attr.Value) if err != nil { return } @@ -353,7 +352,7 @@ func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) { if attr.Value == "" { continue } - i.Left, err = strconv.Atoi(attr.Value) + i.Left, err = GetInt(attr.Value) if err != nil { return } @@ -361,7 +360,7 @@ func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) { if attr.Value == "" { continue } - i.FirstLineChars, err = strconv.Atoi(attr.Value) + i.FirstLineChars, err = GetInt(attr.Value) if err != nil { return } @@ -369,7 +368,7 @@ func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) { if attr.Value == "" { continue } - i.FirstLine, err = strconv.Atoi(attr.Value) + i.FirstLine, err = GetInt(attr.Value) if err != nil { return } @@ -377,7 +376,7 @@ func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) { if attr.Value == "" { continue } - i.HangingChars, err = strconv.Atoi(attr.Value) + i.HangingChars, err = GetInt(attr.Value) if err != nil { return } @@ -385,7 +384,7 @@ func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) { if attr.Value == "" { continue } - i.Hanging, err = strconv.Atoi(attr.Value) + i.Hanging, err = GetInt(attr.Value) if err != nil { return } diff --git a/structpara.go b/structpara.go index 28e9cfc..8d60d7b 100644 --- a/structpara.go +++ b/structpara.go @@ -24,7 +24,6 @@ import ( "encoding/xml" "io" "reflect" - "strconv" "strings" ) @@ -95,7 +94,7 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e if v == "" { continue } - value.Val, err = strconv.ParseInt(v, 10, 64) + value.Val, err = GetInt64(v) if err != nil { return err } @@ -117,7 +116,7 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e if v == "" { continue } - value.Val, err = strconv.Atoi(v) + value.Val, err = GetInt(v) if err != nil { return err } @@ -128,7 +127,7 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e if v == "" { continue } - value.Val, err = strconv.Atoi(v) + value.Val, err = GetInt(v) if err != nil { return err } @@ -139,7 +138,7 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e if v == "" { continue } - value.Val, err = strconv.Atoi(v) + value.Val, err = GetInt(v) if err != nil { return err } @@ -150,7 +149,7 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e if v == "" { continue } - value.Val, err = strconv.Atoi(v) + value.Val, err = GetInt(v) if err != nil { return err } diff --git a/structrun.go b/structrun.go index d949371..7b65895 100644 --- a/structrun.go +++ b/structrun.go @@ -24,7 +24,6 @@ import ( "encoding/xml" "io" "reflect" - "strconv" "strings" ) @@ -289,7 +288,7 @@ func (r *RunProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error { if v == "" { continue } - value.Val, err = strconv.ParseInt(v, 10, 64) + value.Val, err = GetInt64(v) if err != nil { return err } diff --git a/structshape.go b/structshape.go index 7793a72..9298006 100644 --- a/structshape.go +++ b/structshape.go @@ -23,7 +23,6 @@ package docx import ( "encoding/xml" "io" - "strconv" "strings" ) @@ -148,7 +147,7 @@ func (w *WPSCNvSpPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err e for _, attr := range start.Attr { switch attr.Name.Local { case "txBox": - w.TxBox, err = strconv.Atoi(attr.Value) + w.TxBox, err = GetInt(attr.Value) if err != nil { return err } @@ -197,7 +196,7 @@ func (l *ASPLocks) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err for _, attr := range start.Attr { switch attr.Name.Local { case "noChangeArrowheads": - l.NoChangeArrowheads, err = strconv.Atoi(attr.Value) + l.NoChangeArrowheads, err = GetInt(attr.Value) if err != nil { return err } @@ -226,12 +225,12 @@ func (r *ABlipFill) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err er for _, attr := range start.Attr { switch attr.Name.Local { case "dpi": - r.DPI, err = strconv.Atoi(attr.Value) + r.DPI, err = GetInt(attr.Value) if err != nil { return err } case "rotWithShape": - r.RotWithShape, err = strconv.Atoi(attr.Value) + r.RotWithShape, err = GetInt(attr.Value) if err != nil { return err } @@ -300,25 +299,13 @@ func (t *ATile) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) for _, attr := range start.Attr { switch attr.Name.Local { case "tx": - t.TX, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return err - } + t.TX, err = GetInt64(attr.Value) case "ty": - t.TY, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return err - } + t.TY, err = GetInt64(attr.Value) case "sx": - t.SX, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return err - } + t.SX, err = GetInt64(attr.Value) case "sy": - t.SY, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return err - } + t.SY, err = GetInt64(attr.Value) case "flip": t.Flip = attr.Value case "algn": @@ -326,6 +313,9 @@ func (t *ATile) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) default: // ignore other attributes } + if err != nil { + return err + } } // Consume the end element _, err = d.Token() @@ -354,10 +344,7 @@ func (l *ALine) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) for _, attr := range start.Attr { switch attr.Name.Local { case "w": - l.W, err = strconv.ParseInt(attr.Value, 10, 64) - if err != nil { - return err - } + l.W, err = GetInt64(attr.Value) case "cap": l.Cap = attr.Value case "cmpd": @@ -367,6 +354,9 @@ func (l *ALine) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) default: // ignore other attributes } + if err != nil { + return err + } } for { t, err := d.Token() @@ -640,25 +630,25 @@ func (r *WPSBodyPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { for _, attr := range start.Attr { switch attr.Name.Local { case "rot": - r.Rot, _ = strconv.Atoi(attr.Value) + r.Rot, _ = GetInt(attr.Value) case "vert": r.Vert = attr.Value case "wrap": r.Wrap = attr.Value case "lIns": - r.LIns, _ = strconv.ParseInt(attr.Value, 10, 64) + r.LIns, _ = GetInt64(attr.Value) case "tIns": - r.TIns, _ = strconv.ParseInt(attr.Value, 10, 64) + r.TIns, _ = GetInt64(attr.Value) case "rIns": - r.RIns, _ = strconv.ParseInt(attr.Value, 10, 64) + r.RIns, _ = GetInt64(attr.Value) case "bIns": - r.BIns, _ = strconv.ParseInt(attr.Value, 10, 64) + r.BIns, _ = GetInt64(attr.Value) case "anchor": r.Anchor = attr.Value case "anchorCtr": - r.AnchorCtr, _ = strconv.Atoi(attr.Value) + r.AnchorCtr, _ = GetInt(attr.Value) case "upright": - r.Upright, _ = strconv.Atoi(attr.Value) + r.Upright, _ = GetInt(attr.Value) default: // ignore other attributes } diff --git a/structtable.go b/structtable.go index ff3219b..5a2727c 100644 --- a/structtable.go +++ b/structtable.go @@ -23,7 +23,6 @@ package docx import ( "encoding/xml" "io" - "strconv" "strings" ) @@ -203,12 +202,12 @@ func (tp *WTablePositioningProperties) UnmarshalXML(d *xml.Decoder, start xml.St for _, attr := range start.Attr { switch attr.Name.Local { case "leftFromText": - tp.LeftFromText, err = strconv.Atoi(attr.Value) + tp.LeftFromText, err = GetInt(attr.Value) if err != nil { return err } case "rightFromText": - tp.RightFromText, err = strconv.Atoi(attr.Value) + tp.RightFromText, err = GetInt(attr.Value) if err != nil { return err } @@ -221,12 +220,12 @@ func (tp *WTablePositioningProperties) UnmarshalXML(d *xml.Decoder, start xml.St case "tblpYSpec": tp.TblpYSpec = attr.Value case "tblpX": - tp.TblpX, err = strconv.Atoi(attr.Value) + tp.TblpX, err = GetInt(attr.Value) if err != nil { return err } case "tblpY": - tp.TblpY, err = strconv.Atoi(attr.Value) + tp.TblpY, err = GetInt(attr.Value) if err != nil { return err } @@ -277,13 +276,9 @@ func (t *WTableWidth) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err } switch attr.Name.Local { case "w": - t.W, err = strconv.ParseInt(attr.Value, 10, 64) + t.W, err = GetInt64(attr.Value) if err != nil { - w, err := strconv.ParseFloat(attr.Value, 64) - if err != nil { - return err - } - t.W = int64(w) + return err } case "type": t.Type = attr.Value @@ -390,13 +385,9 @@ func (g *WGridCol) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err } switch attr.Name.Local { case "w": - g.W, err = strconv.ParseInt(attr.Value, 10, 64) + g.W, err = GetInt64(attr.Value) if err != nil { - w, err := strconv.ParseFloat(attr.Value, 64) - if err != nil { - return err - } - g.W = int64(w) + return err } default: // ignore other attributes @@ -496,13 +487,9 @@ func (t *WTableRowProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e for _, attr := range tt.Attr { switch attr.Name.Local { case "val": - th.Val, err = strconv.ParseInt(attr.Value, 10, 64) + th.Val, err = GetInt64(attr.Value) if err != nil { - w, err := strconv.ParseFloat(attr.Value, 64) - if err != nil { - return err - } - th.Val = int64(w) + return err } case "hRule": th.Rule = attr.Value @@ -623,13 +610,9 @@ func (r *WTableCellProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) if v == "" { continue } - r.TableCellWidth.W, err = strconv.ParseInt(v, 10, 64) + r.TableCellWidth.W, err = GetInt64(v) if err != nil { - w, err := strconv.ParseFloat(v, 64) - if err != nil { - return err - } - r.TableCellWidth.W = int64(w) + return err } r.TableCellWidth.Type = getAtt(tt.Attr, "type") case "vMerge": @@ -640,7 +623,7 @@ func (r *WTableCellProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) if v == "" { continue } - r.GridSpan.Val, err = strconv.Atoi(v) + r.GridSpan.Val, err = GetInt(v) if err != nil { return err } @@ -805,7 +788,7 @@ func (t *WTableBorder) UnmarshalXML(d *xml.Decoder, start xml.StartElement) erro if attr.Value == "" { continue } - sz, err := strconv.Atoi(attr.Value) + sz, err := GetInt(attr.Value) if err != nil { return err } @@ -814,7 +797,7 @@ func (t *WTableBorder) UnmarshalXML(d *xml.Decoder, start xml.StartElement) erro if attr.Value == "" { continue } - space, err := strconv.Atoi(attr.Value) + space, err := GetInt(attr.Value) if err != nil { return err } diff --git a/structtext.go b/structtext.go index 1039af4..e62c78a 100644 --- a/structtext.go +++ b/structtext.go @@ -24,7 +24,6 @@ import ( "encoding/xml" "io" "reflect" - "strconv" "strings" ) @@ -78,7 +77,7 @@ func (t *Tab) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { if attr.Value == "" { continue } - t.Position, err = strconv.Atoi(attr.Value) + t.Position, err = GetInt(attr.Value) if err != nil { return err }