Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultTmplConstFileData = &TmplConstFileData{ Import: []string{}, PackageName: "protocol", ConstDef: []*generator.ConstValue{}, ErrorDef: []*generator.ConstValue{}, Plugins: make(map[string]interface{}), }
View Source
var DefaultTmplMessageFileData = &TmplMessageFileData{ Import: []string{}, PackageName: "protocol", }
View Source
var TmplConstFile = &generator.TemplateHeader{
Name: "go_const",
Filepath: "go_const.tmpl",
BuiltinTemplateText: `package {{.PackageName}}
{{if (len .Import | ne 0)}}
import ({{range .Import}}{{"\n"}} {{.}}{{end}}
)
{{end}}
{{if (len .ConstDef | ne 0)}}
const ({{range $index, $elem := .ConstDef}}{{"\n"}} {{$elem.Name}} = uint32({{$elem.Value}}){{with (ne $elem.Comment "")}} {{$elem.Comment}}{{end}}{{end}}
)
{{end}}
{{if (len .ErrorDef | ne 0)}}
const ({{range $index, $elem := .ErrorDef}}{{"\n"}} {{$elem.Name}} = uint32({{$elem.Value}}){{with (ne $elem.Comment "")}} {{$elem.Comment}}{{end}}{{end}}
)
{{end}}
`,
}
View Source
var TmplMessageFile = &generator.TemplateHeader{
Name: "go_message",
Filepath: "go_message.tmpl",
BuiltinTemplateText: `package {{.PackageName}}
import (
"fmt"{{range .Import}}{{"\n"}} {{.}}{{end}}
)
type DefineST interface {
Name() string
NewST() DefineST
Encode() ([]byte, error)
Decode([]byte) error
String() string
}
type Message interface {
DefineST
ProtoID() uint32
New() Message
}
var (
mMessageID = make(map[uint32]Message, 256)
mMessageName = make(map[string]Message, 256)
)
func RegMessageDef(def Message) {
if mMessageID[def.ProtoID()] != nil || mMessageName[def.Name()] != nil {
panic(fmt.Errorf("duplicated message define [%s](%d)", def.Name(), def.ProtoID()))
}
mMessageID[def.ProtoID()] = def
mMessageName[def.Name()] = def
}
func New(id uint32) Message {
def, exist := mMessageID[id]
if !exist {
return nil
}
return def.New()
}
func Encode(msg Message) ([]byte, error) {
return msg.Encode()
}
func Decode(id uint32, bs []byte) (Message, error) {
msg := New(id)
if msg == nil {
return nil, fmt.Errorf("unknown message [%d]", id)
}
err := msg.Decode(bs)
if err != nil {
return nil, err
}
return msg, nil
}
`,
}
Functions ¶
func GenCodeForConstValue ¶
func GenCodeForConstValue(pSet *generator.PrototypeSet) (string, error)
func GenCodeForIMessage ¶
func GenFileForConstValue ¶
func GenFileForConstValue(pSet *generator.PrototypeSet) error
func GenFileForIMessage ¶
func GenFileForIMessage() error
Types ¶
type GenConfig ¶
type GenConfig struct { Dir string PackageName string MessageFilename string ConstFilename string Plugins []string }
var GenCFG *GenConfig
type TmplConstFileData ¶ added in v0.2.0
type TmplConstFileData struct { Import []string PackageName string ConstDef []*generator.ConstValue ErrorDef []*generator.ConstValue Plugins map[string]interface{} }
type TmplMessageFileData ¶ added in v0.2.0
Click to show internal directories.
Click to hide internal directories.