2020-04-11 00:49:08 +00:00
|
|
|
package main
|
|
|
|
|
2020-04-16 06:36:34 +00:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-04-11 00:49:08 +00:00
|
|
|
type gotype struct {
|
|
|
|
SliceOf *gotype
|
|
|
|
MapKey *gotype
|
|
|
|
MapVal *gotype
|
|
|
|
PtrTo *gotype
|
2020-04-16 06:36:34 +00:00
|
|
|
|
|
|
|
IsFunc bool
|
|
|
|
FuncParams []gotype
|
|
|
|
FuncReturn []gotype
|
|
|
|
|
|
|
|
Plain string
|
2020-04-11 00:49:08 +00:00
|
|
|
// TODO channel-of (although we will never? construct one from PHP source)
|
|
|
|
}
|
|
|
|
|
2020-04-16 06:36:34 +00:00
|
|
|
func goTypeListToString(gotypes []gotype) string {
|
|
|
|
ret := make([]string, 0, len(gotypes))
|
|
|
|
for _, gt := range gotypes {
|
|
|
|
ret = append(ret, gt.AsGoString())
|
|
|
|
}
|
|
|
|
return strings.Join(ret, `, `)
|
|
|
|
}
|
|
|
|
|
2020-04-11 00:49:08 +00:00
|
|
|
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()
|
|
|
|
|
2020-04-16 06:36:34 +00:00
|
|
|
} else if gt.IsFunc {
|
|
|
|
|
|
|
|
ret := "func(" + goTypeListToString(gt.FuncParams) + `)`
|
|
|
|
if len(gt.FuncReturn) == 1 {
|
|
|
|
ret += ` ` + gt.FuncReturn[0].AsGoString()
|
|
|
|
} else if len(gt.FuncReturn) >= 2 {
|
|
|
|
ret += ` (` + goTypeListToString(gt.FuncReturn) + `)`
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
|
2020-04-11 00:49:08 +00:00
|
|
|
} else {
|
|
|
|
return gt.Plain
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 06:36:34 +00:00
|
|
|
func (gt gotype) IsPlain() bool {
|
|
|
|
if gt.SliceOf != nil || gt.MapKey != nil || gt.PtrTo != nil || gt.IsFunc {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gt gotype) Equals(other *gotype) bool {
|
|
|
|
return gt.AsGoString() == other.AsGoString()
|
|
|
|
}
|
|
|
|
|
2020-04-11 00:49:08 +00:00
|
|
|
// ZeroValue returns a Go literal string for the zero value of this type.
|
|
|
|
func (gt gotype) ZeroValue() string {
|
2020-04-16 06:36:34 +00:00
|
|
|
if !gt.IsPlain() {
|
|
|
|
return `nil`
|
2020-04-11 00:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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() + `{}`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|