-
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.
- Loading branch information
Showing
71 changed files
with
3,631 additions
and
429 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 was deleted.
Oops, something went wrong.
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,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 | ||
} |
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,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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |
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,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 | ||
} |
Oops, something went wrong.