go-jamming/app/webmention/microformats.go

48 lines
1.1 KiB
Go

package webmention
import "willnorris.com/go/microformats"
// Go stuff: entry.Properties["name"][0].(string),
// JS stuff: hEntry.properties?.name?.[0]
// The problem: convoluted syntax and no optional chaining!
func mfstr(mf *microformats.Microformat, key string) string {
val := mf.Properties[key]
if len(val) == 0 {
return ""
}
str, ok := val[0].(string)
if !ok {
// in very weird cases, it could be a map holding a value, like in mf2's "photo"
valMap, ok2 := val[0].(map[string]string)
if !ok2 {
str = ""
}
str = valMap["value"]
}
return str
}
func mfmap(mf *microformats.Microformat, key string) map[string]string {
val := mf.Properties[key]
if len(val) == 0 {
return map[string]string{}
}
mapVal, ok := val[0].(map[string]string)
if !ok {
return map[string]string{}
}
return mapVal
}
func mfprop(mf *microformats.Microformat, key string) *microformats.Microformat {
val := mf.Properties[key]
if len(val) == 0 {
return &microformats.Microformat{
Properties: map[string][]interface{}{},
}
}
return val[0].(*microformats.Microformat)
}