-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ctypes structure and additional para props
- Loading branch information
Showing
90 changed files
with
1,212 additions
and
524 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.