php2go/gotype.go

60 lines
1.1 KiB
Go
Raw Normal View History

package main
type gotype struct {
SliceOf *gotype
MapKey *gotype
MapVal *gotype
PtrTo *gotype
Plain string
// TODO channel-of (although we will never? construct one from PHP source)
}
func (gt gotype) AsGoString() string {
if gt.SliceOf != nil {
return "[]" + gt.SliceOf.AsGoString()
} else if gt.MapKey != nil {
return "map[" + gt.MapKey.AsGoString() + "]" + gt.MapVal.AsGoString()
} else if gt.PtrTo != nil {
return "*" + gt.PtrTo.AsGoString()
} else {
return gt.Plain
}
}
// ZeroValue returns a Go literal string for the zero value of this type.
func (gt gotype) ZeroValue() string {
if gt.SliceOf != nil || gt.MapKey != nil || gt.PtrTo != nil {
return `nil` // gt.AsGoString() + "{}"
}
// It's a plain type
switch gt.Plain {
case "byte",
"int", "int8", "int16", "int32", "int64",
"uint", "uint8", "uint16", "uint32", "uint64",
"float", "float64":
return `0`
case "bool":
return `false`
case "string":
return `""`
case "rune":
return `''`
default:
// probably a struct type
return gt.AsGoString() + `{}`
}
}