Open khpeek opened 1 year ago
One way to work around this (which is probably better pattern in general) is to embed the Location
struct into the Restaurant
and Store
structs rather than vice versa:
package main
import (
"fmt"
"log"
"howett.net/plist"
)
type Location struct {
Name string
Type string
}
type Restaurant struct {
Location
Menu string
}
type Store struct {
Location
Merchandise string
}
func main() {
restaurant := Restaurant{
Location: Location{
Name: "Taqueria Cancun",
Type: "Restaurant",
},
Menu: "Tacos",
}
locationPlist, err := plist.MarshalIndent(&restaurant, plist.XMLFormat, "\t")
if err != nil {
log.Fatalf("marshal location: %v", err)
}
fmt.Println(string(locationPlist))
}
This results in the desired output,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Menu</key>
<string>Tacos</string>
<key>Name</key>
<string>Taqueria Cancun</string>
<key>Type</key>
<string>Restaurant</string>
</dict>
</plist>
However, my point still stands that go-plist
's behavior deviates from that of encoding/json
in this regard.
Thanks for the report, and comprehensive investigation! I'm a bit behind on my personal repositories, but I'll try to get to this this week. You're right - this should work like encoding/json
. There's a chance that this behavior has changed over time and I missed it, or I never mimicked encoding/json
properly. :smile:
Unlike Go's
encoding/json
, it would appear thatgo-plist
still marshals the fields of embedded structs even if those structs arenil
and theomitempty
tag is specified. Consider the following example program:The resulting property list is
This plist representation contains an empty
Merchandise
string which is not pertinent to aRestaurant
and which I'd like to omit. Theencoding/json
library does this as seen from the example below:Running this (and piping the output to
jq
) results inwhere the
Merchandise
key is absent. Should thego-plist
library not also have this behavior?