Skip to content

Commit

Permalink
ctypes structure and additional para props
Browse files Browse the repository at this point in the history
  • Loading branch information
gomutex committed Jun 18, 2024
1 parent c240103 commit 9d0f8a7
Show file tree
Hide file tree
Showing 90 changed files with 1,212 additions and 524 deletions.
10 changes: 5 additions & 5 deletions doc/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package doc
import (
"encoding/xml"

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

// Specifies the background information for this document
//
// This background shall be displayed on all pages of the document, behind all other document content.
type Background struct {
Color *string `xml:"color,attr,omitempty"`
ThemeColor *simpletypes.ThemeColor `xml:"themeColor,attr,omitempty"`
ThemeTint *string `xml:"themeTint,attr,omitempty"`
ThemeShade *string `xml:"themeShade,attr,omitempty"`
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"`
}

func (b *Background) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
Expand Down
8 changes: 4 additions & 4 deletions doc/background_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"testing"

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

func TestBackground_MarshalXML(t *testing.T) {
Expand All @@ -18,7 +18,7 @@ func TestBackground_MarshalXML(t *testing.T) {
name: "With all attributes",
input: Background{
Color: StringPtr("FFFFFF"),
ThemeColor: ThemeColorPtr(simpletypes.ThemeColorAccent1),
ThemeColor: ThemeColorPtr(stypes.ThemeColorAccent1),
ThemeTint: StringPtr("500"),
ThemeShade: StringPtr("200"),
},
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestBackground_UnmarshalXML(t *testing.T) {
inputXML: `<w:background w:color="FFFFFF" w:themeColor="accent1" w:themeTint="500" w:themeShade="200"></w:background>`,
expected: Background{
Color: StringPtr("FFFFFF"),
ThemeColor: ThemeColorPtr(simpletypes.ThemeColorAccent1),
ThemeColor: ThemeColorPtr(stypes.ThemeColorAccent1),
ThemeTint: StringPtr("500"),
ThemeShade: StringPtr("200"),
},
Expand Down Expand Up @@ -134,6 +134,6 @@ func StringPtr(s string) *string {
return &s
}

func ThemeColorPtr(t simpletypes.ThemeColor) *simpletypes.ThemeColor {
func ThemeColorPtr(t stypes.ThemeColor) *stypes.ThemeColor {
return &t
}
4 changes: 2 additions & 2 deletions doc/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"testing"

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

func TestDocument_MarshalXML(t *testing.T) {
Expand All @@ -19,7 +19,7 @@ func TestDocument_MarshalXML(t *testing.T) {
input: Document{
Background: &Background{
Color: StringPtr("FF0000"),
ThemeColor: ThemeColorPtr(simpletypes.ThemeColorAccent1),
ThemeColor: ThemeColorPtr(stypes.ThemeColorAccent1),
ThemeTint: StringPtr("500"),
ThemeShade: StringPtr("200"),
},
Expand Down
21 changes: 21 additions & 0 deletions elemtypes/optOnOffElem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package elemtypes

import (
"encoding/xml"

"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"`
}

// 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 {
if s.Val != "" {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "w:val"}, Value: string(s.Val)})
}
return e.EncodeElement("", start)
}
10 changes: 10 additions & 0 deletions wml/ctypes/empty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ctypes

import "encoding/xml"

type Empty struct {
}

func (s *Empty) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement("", start)
}
63 changes: 63 additions & 0 deletions wml/ctypes/empty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package ctypes

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

func TestEmpty_MarshalXML(t *testing.T) {
tests := []struct {
name string
input Empty
expected string
}{
{
name: "Empty element",
input: Empty{},
expected: `<w:tab></w:tab>`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var result strings.Builder
encoder := xml.NewEncoder(&result)
start := xml.StartElement{Name: xml.Name{Local: "w:tab"}}

err := tt.input.MarshalXML(encoder, start)
if err != nil {
t.Fatalf("Error marshaling XML: %v", err)
}

encoder.Flush()

if result.String() != tt.expected {
t.Errorf("Expected XML:\n%s\nGot:\n%s", tt.expected, result.String())
}
})
}
}

func TestEmpty_UnmarshalXML(t *testing.T) {
tests := []struct {
name string
inputXML string
}{
{
name: "Empty element",
inputXML: `<w:tab></w:tab>`,
},
}

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

err := xml.Unmarshal([]byte(tt.inputXML), &result)
if err != nil {
t.Fatalf("Error unmarshaling XML: %v", err)
}
})
}
}
24 changes: 12 additions & 12 deletions wml/docxrun/shd.go → wml/ctypes/shd.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package docxrun
package ctypes

import (
"encoding/xml"

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

// Shading represents the shading properties for a run in a WordprocessingML document.
type Shading struct {
Val simpletypes.Shading `xml:"val,attr"`
Color *string `xml:"color,attr,omitempty"`
ThemeColor *simpletypes.ThemeColor `xml:"themeColor,attr,omitempty"`
ThemeFill *simpletypes.ThemeColor `xml:"themeFill,attr,omitempty"`
ThemeTint *string `xml:"themeTint,attr,omitempty"`
ThemeShade *string `xml:"themeShade,attr,omitempty"`
Fill *string `xml:"fill,attr,omitempty"`
ThemeFillTint *string `xml:"themeFillTint,attr,omitempty"`
ThemeFillShade *string `xml:"themeFillShade,attr,omitempty"`
Val stypes.Shading `xml:"val,attr"`
Color *string `xml:"color,attr,omitempty"`
ThemeColor *stypes.ThemeColor `xml:"themeColor,attr,omitempty"`
ThemeFill *stypes.ThemeColor `xml:"themeFill,attr,omitempty"`
ThemeTint *string `xml:"themeTint,attr,omitempty"`
ThemeShade *string `xml:"themeShade,attr,omitempty"`
Fill *string `xml:"fill,attr,omitempty"`
ThemeFillTint *string `xml:"themeFillTint,attr,omitempty"`
ThemeFillShade *string `xml:"themeFillShade,attr,omitempty"`
}

func (s *Shading) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
Expand Down Expand Up @@ -80,7 +80,7 @@ func (s *Shading) SetFill(fill string) *Shading {
}

// ShadingType sets the shading type for the shading.
func (s *Shading) SetShadingType(shdType simpletypes.Shading) *Shading {
func (s *Shading) SetShadingType(shdType stypes.Shading) *Shading {
s.Val = shdType
return s
}
24 changes: 12 additions & 12 deletions wml/docxrun/shd_test.go → wml/ctypes/shd_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package docxrun
package ctypes

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

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

func TestShd_MarshalXML(t *testing.T) {
themeColor1 := simpletypes.ThemeColorAccent2
themeFill1 := simpletypes.ThemeColorAccent1
themeColor1 := stypes.ThemeColorAccent2
themeFill1 := stypes.ThemeColorAccent1
tests := []struct {
name string
shading Shading
Expand All @@ -19,14 +19,14 @@ func TestShd_MarshalXML(t *testing.T) {
{
name: "Basic Shd with Val",
shading: Shading{
Val: simpletypes.ShdClear,
Val: stypes.ShdClear,
},
expected: `<w:shd w:val="clear"></w:shd>`,
},
{
name: "Shd with all attributes",
shading: Shading{
Val: simpletypes.ShdSolid,
Val: stypes.ShdSolid,
Color: stringPtr("FFFFFF"),
ThemeColor: &themeColor1,
ThemeFill: &themeFill1,
Expand All @@ -41,7 +41,7 @@ func TestShd_MarshalXML(t *testing.T) {
{
name: "Shd with some attributes nil",
shading: Shading{
Val: simpletypes.ShdDiagStripe,
Val: stypes.ShdDiagStripe,
ThemeColor: &themeColor1,
ThemeFill: &themeFill1,
ThemeTint: stringPtr("500"),
Expand Down Expand Up @@ -73,8 +73,8 @@ func TestShd_MarshalXML(t *testing.T) {
}

func TestShd_UnmarshalXML(t *testing.T) {
themeColor1 := simpletypes.ThemeColorAccent2
themeFill1 := simpletypes.ThemeColorAccent1
themeColor1 := stypes.ThemeColorAccent2
themeFill1 := stypes.ThemeColorAccent1
tests := []struct {
name string
inputXML string
Expand All @@ -84,14 +84,14 @@ func TestShd_UnmarshalXML(t *testing.T) {
name: "Basic Shd with Val",
inputXML: `<w:shd w:val="clear"></w:shd>`,
expectedShd: Shading{
Val: simpletypes.ShdClear,
Val: stypes.ShdClear,
},
},
{
name: "Shd with all attributes",
inputXML: `<w:shd w:val="solid" w:color="FFFFFF" w:themeColor="accent2" w:themeFill="accent1" w:themeTint="500" w:themeShade="200" w:fill="000000" w:themeFillTint="600" w:themeFillShade="300"></w:shd>`,
expectedShd: Shading{
Val: simpletypes.ShdSolid,
Val: stypes.ShdSolid,
Color: stringPtr("FFFFFF"),
ThemeColor: &themeColor1,
ThemeFill: &themeFill1,
Expand All @@ -106,7 +106,7 @@ func TestShd_UnmarshalXML(t *testing.T) {
name: "Shd with some attributes missing",
inputXML: `<w:shd w:val="diagStripe" w:themeColor="accent2" w:themeFill="accent1" w:themeTint="500" w:themeFillTint="600" w:themeFillShade="300"></w:shd>`,
expectedShd: Shading{
Val: simpletypes.ShdDiagStripe,
Val: stypes.ShdDiagStripe,
ThemeColor: &themeColor1,
ThemeFill: &themeFill1,
ThemeTint: stringPtr("500"),
Expand Down
77 changes: 77 additions & 0 deletions wml/ctypes/tab.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ctypes

import (
"encoding/xml"
"fmt"
"strconv"

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

// Custom Tab Stop
type Tab struct {
// Tab Stop Type
Val *stypes.CustTabStop `xml:"val,attr,omitempty"`

//Tab Stop Position
Position *int `xml:"pos,attr,omitempty"`

//Custom Tab Stop Leader Character
LeaderChar *stypes.CustLeadChar `xml:"leader,attr,omitempty"`
}

func (t *Tab) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name.Local = "w:tab"
start.Attr = []xml.Attr{}

if t.Val != nil {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "val"}, Value: string(*t.Val)})
}

if t.Position != nil {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "pos"}, Value: strconv.Itoa(*t.Position)})
}

if t.LeaderChar != nil {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "leader"}, Value: string(*t.LeaderChar)})
}

err := e.EncodeToken(start)
if err != nil {
return err
}

return e.EncodeToken(xml.EndElement{Name: start.Name})
}

type Tabs struct {
Tab []Tab `xml:"tab,omitempty"`
}

func (t Tabs) MarshalXML(e *xml.Encoder, start xml.StartElement) error {

if len(t.Tab) == 0 {
return nil
}

// Create the enclosing XML element
start.Name = xml.Name{Local: "w:tabs"}

err := e.EncodeToken(start)
if err != nil {
return fmt.Errorf("error encoding start element: %v", err)
}

for _, tab := range t.Tab {
if err := tab.MarshalXML(e, xml.StartElement{}); err != nil {
return fmt.Errorf("error encoding tab: %v", err)
}
}

err = e.EncodeToken(start.End())
if err != nil {
return fmt.Errorf("error encoding end element: %v", err)
}

return nil
}
Loading

0 comments on commit 9d0f8a7

Please sign in to comment.