mirror of
https://github.com/Dadido3/go-typst.git
synced 2025-11-07 14:39:36 +00:00
Fix empty struct and map being encoded as empty Typst array
Some checks failed
Some checks failed
This commit is contained in:
parent
513d2ae906
commit
26b40a318d
@ -238,7 +238,7 @@ func (e *ValueEncoder) encodeString(v reflect.Value) error {
|
||||
|
||||
func (e *ValueEncoder) encodeStruct(v reflect.Value, t reflect.Type) error {
|
||||
if v.NumField() == 0 {
|
||||
return e.writeString("()")
|
||||
return e.writeString("(:)")
|
||||
}
|
||||
|
||||
if err := e.writeString("(\n"); err != nil {
|
||||
@ -309,7 +309,7 @@ func (e *ValueEncoder) resolveKeyName(v reflect.Value) (string, error) {
|
||||
|
||||
func (e *ValueEncoder) encodeMap(v reflect.Value) error {
|
||||
if v.Len() == 0 {
|
||||
return e.writeString("()")
|
||||
return e.writeString("(:)")
|
||||
}
|
||||
|
||||
if err := e.writeString("(\n"); err != nil {
|
||||
|
||||
@ -134,11 +134,11 @@ func TestValueEncoder(t *testing.T) {
|
||||
Foo string `typst:"foo"`
|
||||
Bar int `typst:"-"`
|
||||
}{"Hey!", 12345}, false, "(\n \"foo\": \"Hey!\",\n)"},
|
||||
{"struct empty", struct{}{}, false, "()"},
|
||||
{"struct empty", struct{}{}, false, "(:)"},
|
||||
{"struct empty pointer", (*struct{})(nil), false, "none"},
|
||||
{"map string string", map[string]string{"Foo": "Bar", "Foo2": "Electric Foogaloo"}, false, "(\n \"Foo\": \"Bar\",\n \"Foo2\": \"Electric Foogaloo\",\n)"},
|
||||
{"map string string empty", map[string]string{}, false, "()"},
|
||||
{"map string string nil", map[string]string(nil), false, "()"},
|
||||
{"map string string empty", map[string]string{}, false, "(:)"},
|
||||
{"map string string nil", map[string]string(nil), false, "(:)"},
|
||||
{"map string string key escape", map[string]string{"A\nNew": "Line"}, false, "(\n \"A\\nNew\": \"Line\",\n)"},
|
||||
{"map int string", map[int]string{1: "Foo", 2: "Bar"}, false, "(\n \"1\": \"Foo\",\n \"2\": \"Bar\",\n)"},
|
||||
{"map int string negative keys", map[int]string{-1: "Foo", -2: "Bar"}, false, "(\n \"-1\": \"Foo\",\n \"-2\": \"Bar\",\n)"},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user