Skip to content

Commit

Permalink
implementation of paragraph props
Browse files Browse the repository at this point in the history
  • Loading branch information
gomutex committed Jun 19, 2024
1 parent 6fcdba1 commit 1137a74
Show file tree
Hide file tree
Showing 71 changed files with 3,631 additions and 429 deletions.
12 changes: 6 additions & 6 deletions elemtypes/optOnOffElem.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"github.com/gomutex/godocx/wml/stypes"
)

// OptOnOffElem helper struct that has only one optional field which is OnOff type
type OptOnOffElem struct {
Val stypes.OnOff `xml:"val,attr,omitempty"`
// OptBinFlagElem helper struct that has only one optional field which is BinFlag type
type OptBinFlagElem struct {
Val stypes.BinFlag `xml:"val,attr,omitempty"`
}

// MarshalXML implements the xml.Marshaler interface for the OnOffElem type.
// It encodes the OnOffElem to its corresponding XML representation.
func (s *OptOnOffElem) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
// MarshalXML implements the xml.Marshaler interface for the BinFlagElem type.
// It encodes the BinFlagElem to its corresponding XML representation.
func (s *OptBinFlagElem) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if s.Val != "" {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "w:val"}, Value: string(s.Val)})
}
Expand Down
28 changes: 0 additions & 28 deletions elemtypes/singleIntVal.go

This file was deleted.

49 changes: 49 additions & 0 deletions internal/testhelper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package internal

import (
"fmt"
"reflect"
)

func ToPtr[T any](input T) *T {
return &input
}

func FormatPtr[T any](ptr *T) string {
if ptr == nil {
return "<nil>"
}
return fmt.Sprintf("%v", *ptr)
}

// func ComparePtr[T comparable](fieldName string, a, b *T) error {
// if a == nil || b == nil {
// if a != b {
// return fmt.Errorf("%s: expected %v but got %v", fieldName, FormatPtr(a), FormatPtr(b))
// }
// } else if *a != *b {
// return fmt.Errorf("%s: expected %v but got %v", fieldName, *a, *b)
// }
// return nil
// }

func ComparePtr[T comparable](fieldName string, a, b *T) error {
// Check if T is a struct
if reflect.TypeOf(*new(T)).Kind() == reflect.Struct {
if a == nil || b == nil {
if a != b {
return fmt.Errorf("%s: expected %v but got %v", fieldName, FormatPtr(a), FormatPtr(b))
}
}
} else {
// For non-struct types, perform value comparison
if a == nil || b == nil {
if a != b {
return fmt.Errorf("%s: expected %v but got %v", fieldName, FormatPtr(a), FormatPtr(b))
}
} else if *a != *b {
return fmt.Errorf("%s: expected %v but got %v", fieldName, *a, *b)
}
}
return nil
}
10 changes: 5 additions & 5 deletions wml/docxrun/border.go → wml/ctypes/border.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package docxrun
package ctypes

import (
"encoding/xml"

"github.com/gomutex/godocx/wml/stypes"
)

type TextBorder struct {
type Border struct {
Val stypes.BorderStyle `xml:"val,attr"`
Color *string `xml:"color,attr,omitempty"`
ThemeColor *stypes.ThemeColor `xml:"themeColor,attr,omitempty"`
ThemeTint *string `xml:"themeTint,attr,omitempty"`
ThemeShade *string `xml:"themeShade,attr,omitempty"`
Space *string `xml:"space,attr,omitempty"`
Shadow *stypes.OnOff `xml:"shadow,attr,omitempty"`
Frame *stypes.OnOff `xml:"frame,attr,omitempty"`
Shadow *stypes.BinFlag `xml:"shadow,attr,omitempty"`
Frame *stypes.BinFlag `xml:"frame,attr,omitempty"`
}

func (t *TextBorder) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
func (t *Border) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "w:val"}, Value: string(t.Val)})

if t.Color != nil {
Expand Down
30 changes: 15 additions & 15 deletions wml/docxrun/border_test.go → wml/ctypes/border_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package docxrun
package ctypes

import (
"encoding/xml"
Expand All @@ -8,29 +8,29 @@ import (
"github.com/gomutex/godocx/wml/stypes"
)

func TestTextBorder_MarshalXML(t *testing.T) {
func TestBorder_MarshalXML(t *testing.T) {
tests := []struct {
name string
input TextBorder
input Border
expected string
}{
{
name: "With all attributes",
input: TextBorder{
input: Border{
Val: stypes.BorderStyleSingle,
Color: StringPtr("FF0000"),
ThemeColor: themeColorPointer(stypes.ThemeColorAccent1),
ThemeTint: StringPtr("500"),
ThemeShade: StringPtr("200"),
Space: StringPtr("0"),
Shadow: OnOffPtr(stypes.OnOffTrue),
Frame: OnOffPtr(stypes.OnOffTrue),
Shadow: BinFlagPtr(stypes.BinFlagTrue),
Frame: BinFlagPtr(stypes.BinFlagTrue),
},
expected: `<w:bdr w:val="single" w:color="FF0000" w:themeColor="accent1" w:themeTint="500" w:themeShade="200" w:space="0" w:shadow="true" w:frame="true"></w:bdr>`,
},
{
name: "Without optional attributes",
input: TextBorder{
input: Border{
Val: stypes.BorderStyleDouble,
},
expected: `<w:bdr w:val="double"></w:bdr>`,
Expand All @@ -57,39 +57,39 @@ func TestTextBorder_MarshalXML(t *testing.T) {
}
}

func TestTextBorder_UnmarshalXML(t *testing.T) {
func TestBorder_UnmarshalXML(t *testing.T) {
tests := []struct {
name string
inputXML string
expected TextBorder
expected Border
}{
{
name: "With all attributes",
inputXML: `<w:bdr w:val="single" w:color="FF0000" w:themeColor="accent1" w:themeTint="500" ` +
`w:themeShade="200" w:space="0" w:shadow="true" w:frame="true"></w:bdr>`,
expected: TextBorder{
expected: Border{
Val: stypes.BorderStyleSingle,
Color: StringPtr("FF0000"),
ThemeColor: themeColorPointer(stypes.ThemeColorAccent1),
ThemeTint: StringPtr("500"),
ThemeShade: StringPtr("200"),
Space: StringPtr("0"),
Shadow: OnOffPtr(stypes.OnOffTrue),
Frame: OnOffPtr(stypes.OnOffTrue),
Shadow: BinFlagPtr(stypes.BinFlagTrue),
Frame: BinFlagPtr(stypes.BinFlagTrue),
},
},
{
name: "Without optional attributes",
inputXML: `<w:bdr w:val="double"></w:bdr>`,
expected: TextBorder{
expected: Border{
Val: stypes.BorderStyleDouble,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var result TextBorder
var result Border

err := xml.Unmarshal([]byte(tt.inputXML), &result)
if err != nil {
Expand Down Expand Up @@ -171,7 +171,7 @@ func StringPtr(s string) *string {
return &s
}

func OnOffPtr(o stypes.OnOff) *stypes.OnOff {
func BinFlagPtr(o stypes.BinFlag) *stypes.BinFlag {
return &o
}

Expand Down
17 changes: 17 additions & 0 deletions wml/ctypes/cnf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ctypes

import (
"encoding/xml"
)

type Cnf struct {
Val string `xml:"val,attr"`
}

func (c *Cnf) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name.Local = "w:cnfStyle"
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "w:val"}, Value: c.Val})
err := e.EncodeElement("", start)

return err
}
71 changes: 71 additions & 0 deletions wml/ctypes/cnf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ctypes

import (
"encoding/xml"
"strings"
"testing"
)

func TestCnf_MarshalXML(t *testing.T) {
tests := []struct {
name string
cnf Cnf
expected string
}{
{
name: "WithVal",
cnf: Cnf{Val: "12345"},
expected: `<w:cnfStyle w:val="12345"></w:cnfStyle>`,
},
{
name: "EmptyVal",
cnf: Cnf{Val: ""},
expected: `<w:cnfStyle w:val=""></w:cnfStyle>`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
xmlBytes, err := xml.Marshal(&tt.cnf)
if err != nil {
t.Fatalf("Error marshaling Cnf: %v", err)
}

result := string(xmlBytes)
if !strings.Contains(result, tt.expected) {
t.Errorf("Expected XML:\n%s\nBut got:\n%s", tt.expected, result)
}
})
}
}

func TestCnf_UnmarshalXML(t *testing.T) {
tests := []struct {
xmlStr string
expected Cnf
}{
{
xmlStr: `<w:cnfStyle w:val="12345"></w:cnfStyle>`,
expected: Cnf{Val: "12345"},
},
{
xmlStr: `<w:cnfStyle w:val=""></w:cnfStyle>`,
expected: Cnf{Val: ""},
},
}

for _, tt := range tests {
t.Run(tt.xmlStr, func(t *testing.T) {
var cnf Cnf

err := xml.Unmarshal([]byte(tt.xmlStr), &cnf)
if err != nil {
t.Fatalf("Error unmarshaling XML: %v", err)
}

if cnf.Val != tt.expected.Val {
t.Errorf("Expected Val %s but got %s", tt.expected.Val, cnf.Val)
}
})
}
}
23 changes: 23 additions & 0 deletions wml/ctypes/decNum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ctypes

import (
"encoding/xml"
"strconv"
)

type DecimalNum struct {
Val int `xml:"val,attr"`
}

func NewDecimalNum(value int) *DecimalNum {
return &DecimalNum{
Val: value,
}
}

func (s *DecimalNum) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "w:val"}, Value: strconv.Itoa(s.Val)})
err := e.EncodeElement("", start)

return err
}
Loading

0 comments on commit 1137a74

Please sign in to comment.