Ejemplo n.º 1
0
func (codegen *_CodeGen) Contract(compiler *gslang.Compiler, contract *ast.Contract) {

	var buff bytes.Buffer

	if err := codegen.tpl.ExecuteTemplate(&buff, "contract", contract); err != nil {
		gserrors.Panicf(err, "exec template(contract) for %s error", contract)
	}

	codegen.writeJavaFile(contract.Name(), contract, buff.Bytes())

	buff.Reset()

	if err := codegen.tpl.ExecuteTemplate(&buff, "dispatcher", contract); err != nil {
		gserrors.Panicf(err, "exec template(contract) for %s error", contract)
	}

	codegen.writeJavaFile(contract.Name()+"Dispatcher", contract, buff.Bytes())

	buff.Reset()

	if err := codegen.tpl.ExecuteTemplate(&buff, "rpc", contract); err != nil {
		gserrors.Panicf(err, "exec template(contract) for %s error", contract)
	}

	codegen.writeJavaFile(contract.Name()+"RPC", contract, buff.Bytes())
}
Ejemplo n.º 2
0
func (codegen *_CodeGen) writeJavaFile(name string, expr ast.Expr, content []byte) {

	var buff bytes.Buffer

	jPackageName := javaPackageName(codegen.packageName)

	packagename := strings.Replace(jPackageName, ".", "/", -1)

	fullpath := filepath.Join(codegen.rootpath, packagename, name+".java")

	buff.WriteString(fmt.Sprintf("package %s;\n\n", jPackageName))

	for _, i := range codegen.imports {

		if name == "EvtRPC" {
			codegen.I("%s", i)
		}

		buff.WriteString(fmt.Sprintf("%s;\n\n", i))

	}

	buff.Write(content)

	if err := os.MkdirAll(filepath.Dir(fullpath), 0755); err != nil {
		gserrors.Panicf(err, "create output directory error")
	}

	codegen.D("write file :%s", fullpath)

	if err := ioutil.WriteFile(fullpath, buff.Bytes(), 0644); err != nil {
		gserrors.Panicf(err, "write generate stub code error")
	}
}
Ejemplo n.º 3
0
Archivo: main.go Proyecto: gsrpc/gsrpc
func main() {
	gslogger.Console("$content", "2006-01-02 15:04:05.999999")
	gslogger.NewFlags(gslogger.ERROR | gslogger.WARN | gslogger.INFO)
	log := gslogger.Get("gsrpc")

	defer func() {
		if e := recover(); e != nil {
			log.E("%s", e)
		}

		gslogger.Join()
	}()

	flag.Parse()

	*output, _ = filepath.Abs(*output)

	log.I("Start gsRPC With Target Language(%s)", *lang)

	codegenF, ok := langs[*lang]

	if !ok {
		log.E("unknown gsrpc object language :%s", *lang)
		os.Exit(1)
	}

	codegen, err := codegenF(*output, []string{"github.com/gsrpc/gslang"})

	if err != nil {
		gserrors.Panicf(err, "create language(%s) codegen error", *lang)
	}

	compiler := gslang.NewCompiler("gsrpc", gslang.HandleError(func(err *gslang.Error) {
		gserrors.Panicf(err.Orignal, "parse %s error\n\t%s", err.Start, err.Text)
	}))

	for _, file := range flag.Args() {
		log.I("Compile gsLang File :%s", file)
		if err := compiler.Compile(file); err != nil {
			gserrors.Panicf(err, "compile %s error", file)
		}
	}

	log.I("Link ...")
	err = compiler.Link()

	if err != nil {
		gserrors.Panicf(err, "link error")
	}

	log.I("Output Directory :%s", *output)

	if err := compiler.Visit(codegen); err != nil {
		gserrors.Panicf(err, "generate language codes(%s) error", *lang)
	}

	log.I("Run gsRPC Compile -- Success")
}
Ejemplo n.º 4
0
func (codegen *_CodeGen) Contract(compiler *gslang.Compiler, contract *ast.Contract) {
	if err := codegen.tpl.ExecuteTemplate(&codegen.header, "contract_header", contract); err != nil {
		gserrors.Panicf(err, "exec template(Contract) for %s error", contract)
	}

	if err := codegen.tpl.ExecuteTemplate(&codegen.source, "contract_source", contract); err != nil {
		gserrors.Panicf(err, "exec template(Contract) for %s error", contract)
	}
}
Ejemplo n.º 5
0
func (registry *_Registry) Load(origin io.Reader, name string) {
	reader := bufio.NewReader(origin)

	lines := 0

	items := make(map[string]uint16)

	for {
		line, err := reader.ReadString('\n')

		if err != nil {
			if err == io.EOF {
				break
			}

			gserrors.Panicf(err, "read registry file error :%s", name)
		}

		tokens := registryRegex.FindStringSubmatch(line)

		if tokens == nil {
			gserrors.Panicf(ErrRegistry, "load registry file error: invalid format\n\t%s(%d)", name, lines)
		}

		serviceName := ""

		serviceID := uint16(0)

		for i, name := range registryRegex.SubexpNames() {
			if name == "name" {
				serviceName = tokens[i]
			}

			if name == "id" {
				val, err := strconv.ParseInt(tokens[i], 0, 32)

				if err != nil {
					gserrors.Panicf(err, "load registry file error: invalid format\n\t%s(%d)", name, lines)
				}

				if val > math.MaxUint16 {
					gserrors.Panicf(ErrRegistry, "load registry file error: id out of range\n\t%s(%d)", name, lines)
				}

				serviceID = uint16(val)
			}
		}

		items[serviceName] = serviceID

		lines++

	}

	registry.Update(items)
}
Ejemplo n.º 6
0
func (codegen *_CodeGen) defaultVal(typeDecl ast.Type) string {

	switch typeDecl.(type) {
	case *ast.BuiltinType:
		builtinType := typeDecl.(*ast.BuiltinType)
		return defaultval[builtinType.Type]
	case *ast.TypeRef:
		typeRef := typeDecl.(*ast.TypeRef)

		return codegen.defaultVal(typeRef.Ref)

	case *ast.Enum:

		enum := typeDecl.(*ast.Enum)

		prefix, name := codegen.typeRef(typeDecl.Package(), typeDecl.FullName())

		if prefix != "" {
			return prefix + "." + name + "" + enum.Constants[0].Name()
		}

		return name + "" + enum.Constants[0].Name()

	case *ast.Table:

		prefix, name := codegen.typeRef(typeDecl.Package(), typeDecl.FullName())

		if prefix != "" {
			return prefix + ".New" + name + "()"
		}

		return "New" + name + "()"

	case *ast.Seq:
		seq := typeDecl.(*ast.Seq)

		if seq.Size != -1 {

			var buff bytes.Buffer

			if err := codegen.tpl.ExecuteTemplate(&buff, "create_array", seq); err != nil {
				gserrors.Panicf(err, "exec template(create_array) for %s errir", seq)
			}

			return buff.String()
		}

		return "nil"
	}

	gserrors.Panicf(nil, "typeName  error: unsupport type(%s)", typeDecl)

	return "unknown"
}
Ejemplo n.º 7
0
func (runner *_Runner) checkName(name, fullname string) {
	if _, ok := runner.fullname[name]; ok {
		gserrors.Panicf(ErrFlag, "duplicate flag name :%s", name)
	}

	for _, v := range runner.fullname {
		if v == fullname {
			gserrors.Panicf(ErrFlag, "duplicate flag fullname :%s", fullname)
		}
	}

	runner.fullname[name] = fullname
}
Ejemplo n.º 8
0
func (codegen *_CodeGen) writefile(bytes []byte, extend string) {

	path := strings.Replace(codegen.script.Package, ".", "/", -1)

	fullpath := filepath.Join(codegen.rootpath, path, filepath.Base(codegen.script.Name())+extend)

	if err := os.MkdirAll(filepath.Dir(fullpath), 0755); err != nil {
		gserrors.Panicf(err, "create output directory error")
	}

	if err := ioutil.WriteFile(fullpath, bytes, 0644); err != nil {
		gserrors.Panicf(err, "write generate stub code error")
	}
}
Ejemplo n.º 9
0
func (codegen *_CodeGen) tagValue(typeDecl ast.Type) string {
	switch typeDecl.(type) {
	case *ast.BuiltinType:
		builtinType := typeDecl.(*ast.BuiltinType)

		switch builtinType.Type {
		case lexer.KeySByte, lexer.KeyByte, lexer.KeyBool:
			return "GSTagI8"
		case lexer.KeyInt16, lexer.KeyUInt16:
			return "GSTagI16"
		case lexer.KeyInt32, lexer.KeyUInt32, lexer.KeyFloat32:
			return "GSTagI32"
		case lexer.KeyInt64, lexer.KeyUInt64, lexer.KeyFloat64:
			return "GSTagI64"
		case lexer.KeyString:
			return "GSTagString"
		}

	case *ast.TypeRef:
		return codegen.tagValue(typeDecl.(*ast.TypeRef).Ref)
	case *ast.Enum:

		_, ok := gslang.FindAnnotation(typeDecl, "gslang.Flag")

		if !ok {
			return "GSTagI32"
		}

		return "GSTagI8"

	case *ast.Table:
		return "GSTagTable"
	case *ast.Seq:

		seq := typeDecl.(*ast.Seq)

		component := codegen.tagValue(seq.Component)

		if component == "GSTagList" {
			start, _ := gslang.Pos(typeDecl)
			gserrors.Panicf(nil, "list component %v can't be a list :%v", seq.Component, start)
		}

		return fmt.Sprintf("((%s << 4)|GSTagList)", component)
	}

	gserrors.Panicf(nil, "typeName  error: unsupport type(%s)", typeDecl)

	return ""
}
Ejemplo n.º 10
0
func (codegen *_CodeGen) Enum(compiler *gslang.Compiler, enum *ast.Enum) {

	if err := codegen.tpl.ExecuteTemplate(&codegen.predecl, "enum_predecl", enum); err != nil {
		gserrors.Panicf(err, "exec template(enum_predecl) for %s error", enum)
	}

	if err := codegen.tpl.ExecuteTemplate(&codegen.header, "enum_header", enum); err != nil {
		gserrors.Panicf(err, "exec template(Enum) for %s error", enum)
	}

	if err := codegen.tpl.ExecuteTemplate(&codegen.source, "enum_source", enum); err != nil {
		gserrors.Panicf(err, "exec template(Enum) for %s error", enum)
	}
}
Ejemplo n.º 11
0
func (codegen *_CodeGen) Table(compiler *gslang.Compiler, tableType *ast.Table) {

	if err := codegen.tpl.ExecuteTemplate(&codegen.predecl, "table_predecl", tableType); err != nil {
		gserrors.Panicf(err, "exec template(table_predecl) for %s error", tableType)
	}

	if err := codegen.tpl.ExecuteTemplate(&codegen.header, "table_header", tableType); err != nil {

		gserrors.Panicf(err, "exec template(table) for %s error", tableType)
	}

	if err := codegen.tpl.ExecuteTemplate(&codegen.source, "table_source", tableType); err != nil {
		gserrors.Panicf(err, "exec template(table) for %s error", tableType)
	}
}
Ejemplo n.º 12
0
func (codegen *_CodeGen) tagValue(typeDecl ast.Type) string {
	switch typeDecl.(type) {
	case *ast.BuiltinType:
		builtinType := typeDecl.(*ast.BuiltinType)

		switch builtinType.Type {
		case lexer.KeySByte, lexer.KeyByte, lexer.KeyBool:
			return "com.gsrpc.Tag.I8.getValue()"
		case lexer.KeyInt16, lexer.KeyUInt16:
			return "com.gsrpc.Tag.I16.getValue()"
		case lexer.KeyInt32, lexer.KeyUInt32, lexer.KeyFloat32:
			return "com.gsrpc.Tag.I32.getValue()"
		case lexer.KeyInt64, lexer.KeyUInt64, lexer.KeyFloat64:
			return "com.gsrpc.Tag.I64.getValue()"
		case lexer.KeyString:
			return "com.gsrpc.Tag.String.getValue()"
		}

	case *ast.TypeRef:
		return codegen.tagValue(typeDecl.(*ast.TypeRef).Ref)
	case *ast.Enum:

		if codegen.enumSize(typeDecl) == 4 {
			return "com.gsrpc.Tag.I32.getValue()"
		}

		return "com.gsrpc.Tag.I8.getValue()"

	case *ast.Table:
		return "com.gsrpc.Tag.Table.getValue()"
	case *ast.Seq:

		seq := typeDecl.(*ast.Seq)

		component := codegen.tagValue(seq.Component)

		if component == "com.gsrpc.Tag.List.getValue()" {
			start, _ := gslang.Pos(typeDecl)
			gserrors.Panicf(nil, "list component %v can't be a list :%v", seq.Component, start)
		}

		return fmt.Sprintf("((%s << 4)|com.gsrpc.Tag.List.getValue())", component)
	}

	gserrors.Panicf(nil, "typeName  error: unsupport type(%s)", typeDecl)

	return ""
}
Ejemplo n.º 13
0
func (codegen *_CodeGen) Table(compiler *gslang.Compiler, tableType *ast.Table) {

	if err := codegen.tpl.ExecuteTemplate(&codegen.content, "table", tableType); err != nil {
		gserrors.Panicf(err, "exec template(table) for %s errir", tableType)
	}

}
Ejemplo n.º 14
0
func (codegen *_CodeGen) objTypeName(typeDecl ast.Type) string {
	switch typeDecl.(type) {
	case *ast.BuiltinType:
		builtinType := typeDecl.(*ast.BuiltinType)

		return builtinObj[builtinType.Type]
	case *ast.TypeRef:
		typeRef := typeDecl.(*ast.TypeRef)

		return codegen.typeName(typeRef.Ref)

	case *ast.Enum, *ast.Table:
		prefix, name := codegen.typeRef(typeDecl.Package(), typeDecl.FullName())

		if prefix != "" {
			return prefix + "." + name
		}

		return name

	case *ast.Seq:
		seq := typeDecl.(*ast.Seq)

		return fmt.Sprintf("%s[]", codegen.typeName(seq.Component))
	}

	gserrors.Panicf(nil, "typeName  error: unsupport type(%s)", typeDecl)

	return "unknown"
}
Ejemplo n.º 15
0
func (codegen *_CodeGen) typeName(typeDecl ast.Type) string {
	switch typeDecl.(type) {
	case *ast.BuiltinType:
		builtinType := typeDecl.(*ast.BuiltinType)

		return builtin[builtinType.Type]
	case *ast.TypeRef:
		typeRef := typeDecl.(*ast.TypeRef)

		return codegen.typeName(typeRef.Ref)

	case *ast.Enum, *ast.Table:
		_, name := codegen.typeRef(typeDecl.Package(), typeDecl.FullName())

		if gslang.IsException(typeDecl) {
			return exception(name)
		}

		return name

	case *ast.Seq:
		seq := typeDecl.(*ast.Seq)

		return fmt.Sprintf("%s[]", codegen.typeName(seq.Component))
	}

	gserrors.Panicf(nil, "typeName  error: unsupport type(%s)", typeDecl)

	return "unknown"
}
Ejemplo n.º 16
0
func TestParser(t *testing.T) {

	defer gslogger.Join()

	compiler := gslang.NewCompiler("test", gslang.HandleError(func(err *gslang.Error) {
		gserrors.Panicf(err.Orignal, "parse %s error\n\t%s", err.Start, err.Text)
	}))

	err := compiler.Compile("test.gs")

	if err != nil {
		t.Fatal(err)
	}

	err = compiler.Compile("../gslang.gs")

	if err != nil {
		t.Fatal(err)
	}

	err = compiler.Compile("../annotations.gs")

	if err != nil {
		t.Fatal(err)
	}

	err = compiler.Link()

	if err != nil {
		t.Fatal(err)
	}
}
Ejemplo n.º 17
0
func (codegen *_CodeGen) defaultVal(typeDecl ast.Type) string {

	switch typeDecl.(type) {
	case *ast.BuiltinType:
		builtinType := typeDecl.(*ast.BuiltinType)
		return defaultval[builtinType.Type]
	case *ast.TypeRef:
		typeRef := typeDecl.(*ast.TypeRef)

		return codegen.defaultVal(typeRef.Ref)

	case *ast.Enum:

		enum := typeDecl.(*ast.Enum)

		_, name := codegen.typeRef(typeDecl.Package(), typeDecl.FullName())
		//
		// if prefix != "" {
		// 	return prefix + "." + name + "." + enum.Constants[0].Name()
		// }

		return name + "." + enum.Constants[0].Name()

	case *ast.Table:

		return "new " + codegen.typeName(typeDecl) + "()"

	case *ast.Seq:
		return fmt.Sprintf("new %s", codegen.arrayDefaultVal(typeDecl))
	}

	gserrors.Panicf(nil, "typeName  error: unsupport type(%s)", typeDecl)

	return "unknown"
}
Ejemplo n.º 18
0
// EndScript .
func (codegen *_CodeGen) EndScript(compiler *gslang.Compiler) {

	content := codegen.content.String()

	packageName := codegen.script.Package

	if packageName == "com.gsrpc" {
		content = strings.Replace(content, "gorpc.", "", -1)
	}

	for k, v := range imports {
		if strings.Contains(content, k) {
			codegen.header.WriteString(fmt.Sprintf("import \"%s\"\n", v))
		}
	}

	codegen.header.WriteString(content)

	var err error
	var sources []byte

	fullpath := filepath.Join(codegen.rootpath, codegen.scriptPath, filepath.Base(codegen.script.Name())+".go")

	sources, err = format.Source(codegen.header.Bytes())

	if err != nil {
		gserrors.Panicf(err, "format golang source codes error:%s", fullpath)
	}

	codegen.D("generate golang file :%s", fullpath)

	if !fs.Exists(filepath.Dir(fullpath)) {
		err := os.MkdirAll(filepath.Dir(fullpath), 0755)

		if err != nil {
			gserrors.Panicf(err, "format golang source codes error")
		}
	}

	err = ioutil.WriteFile(fullpath, sources, 0644)

	if err != nil {
		gserrors.Panicf(err, "write generate golang file error")
	}
}
Ejemplo n.º 19
0
func (dataset *_DataSet) Next() (data []byte, version uint64) {
	select {
	case val := <-dataset.Q:
		return val.Content, val.ID
	case <-dataset.closed:
		gserrors.Panicf(ErrOp, "call Next on closed dataset")
		return nil, 0
	}
}
Ejemplo n.º 20
0
func (codegen *_CodeGen) Enum(compiler *gslang.Compiler, enum *ast.Enum) {

	var buff bytes.Buffer

	if err := codegen.tpl.ExecuteTemplate(&buff, "enum", enum); err != nil {
		gserrors.Panicf(err, "exec template(enum) for %s error", enum)
	}

	codegen.writeJavaFile(enum.Name(), enum, buff.Bytes())
}
Ejemplo n.º 21
0
func (registry *_Registry) ServiceName(id uint16) string {
	registry.RLock()
	defer registry.RUnlock()

	if val, ok := registry.id[id]; ok {
		return val
	}

	gserrors.Panicf(ErrRegistryNotFound, "can't found service name for %s", id)

	return ""
}
Ejemplo n.º 22
0
func (registry *_Registry) ServiceID(name string) uint16 {
	registry.RLock()
	defer registry.RUnlock()

	if val, ok := registry.name[name]; ok {
		return val
	}

	gserrors.Panicf(ErrRegistryNotFound, "can't found service id for %s", name)

	return 0
}
Ejemplo n.º 23
0
func (codegen *_CodeGen) Table(compiler *gslang.Compiler, tableType *ast.Table) {

	var buff bytes.Buffer

	if err := codegen.tpl.ExecuteTemplate(&buff, "table", tableType); err != nil {
		gserrors.Panicf(err, "exec template(table) for %s error", tableType)
	}

	if gslang.IsException(tableType) {
		codegen.writeJavaFile(exception(tableType.Name()), tableType, buff.Bytes())
	} else {
		codegen.writeJavaFile(tableType.Name(), tableType, buff.Bytes())
	}

}
Ejemplo n.º 24
0
func (codegen *_CodeGen) typeName(typeDecl ast.Type) string {
	switch typeDecl.(type) {
	case *ast.BuiltinType:

		builtinType := typeDecl.(*ast.BuiltinType)

		return builtin[builtinType.Type]

	case *ast.TypeRef:
		typeRef := typeDecl.(*ast.TypeRef)

		return codegen.typeName(typeRef.Ref)

	case *ast.Enum:
		return codegen.title(typeDecl.(ast.TypeDecl))
	case *ast.Table:

		return codegen.title(typeDecl.(ast.TypeDecl)) + "*"

	case *ast.Seq:
		seq := typeDecl.(*ast.Seq)

		isbytes := false

		builtinType, ok := seq.Component.(*ast.BuiltinType)

		if ok && builtinType.Type == lexer.KeyByte {
			isbytes = true
		}

		if isbytes {
			return "NSMutableData *"
		}

		return "NSMutableArray *"
	}

	gserrors.Panicf(nil, "typeName  error: unsupport type(%s)", typeDecl)

	return "unknown"
}
Ejemplo n.º 25
0
func (codegen *_CodeGen) Contract(compiler *gslang.Compiler, contract *ast.Contract) {
	if err := codegen.tpl.ExecuteTemplate(&codegen.content, "contract", contract); err != nil {
		gserrors.Panicf(err, "exec template(contract) for %s errir", contract)
	}
}
Ejemplo n.º 26
0
func (codegen *_CodeGen) Enum(compiler *gslang.Compiler, enum *ast.Enum) {
	if err := codegen.tpl.ExecuteTemplate(&codegen.content, "enum", enum); err != nil {
		gserrors.Panicf(err, "exec template(enum) for %s errir", enum)
	}
}
Ejemplo n.º 27
0
func (codegen *_CodeGen) readType(valname string, typeDecl ast.Type, indent int) string {
	switch typeDecl.(type) {
	case *ast.BuiltinType:
		builtinType := typeDecl.(*ast.BuiltinType)
		return fmt.Sprintf("%s = %s();", valname, readMapping[builtinType.Type])
	case *ast.TypeRef:
		typeRef := typeDecl.(*ast.TypeRef)

		return codegen.readType(valname, typeRef.Ref, indent)

	case *ast.Enum:
		return fmt.Sprintf("%s = %s.unmarshal(reader);", valname, codegen.typeName(typeDecl))

	case *ast.Table:
		return fmt.Sprintf("%s.unmarshal(reader);", valname)

	case *ast.Seq:
		seq := typeDecl.(*ast.Seq)

		isbytes := false

		builtinType, ok := seq.Component.(*ast.BuiltinType)

		if ok && builtinType.Type == lexer.KeyByte {
			isbytes = true
		}

		if seq.Size == -1 {

			if isbytes {
				return fmt.Sprintf("%s = reader.readBytes();", valname)
			}

			var stream bytes.Buffer

			stream.WriteString(fmt.Sprintf("int max%d = reader.readUInt16();\n\n", indent))

			writeindent(&stream, indent-1)

			stream.WriteString(fmt.Sprintf("%s = new %s[max%d];\n\n", valname, codegen.arrayDefaultVal(seq.Component), indent))

			writeindent(&stream, indent-1)

			stream.WriteString(fmt.Sprintf("for(int i%d = 0; i%d < max%d; i%d ++ ){\n\n", indent, indent, indent, indent))

			writeindent(&stream, indent)

			stream.WriteString(fmt.Sprintf("%s v%d = %s;\n\n", codegen.typeName(seq.Component), indent, codegen.defaultVal(seq.Component)))

			writeindent(&stream, indent)

			stream.WriteString(codegen.readType(fmt.Sprintf("v%d", indent), seq.Component, indent+1))

			stream.WriteString("\n\n")

			writeindent(&stream, indent)

			stream.WriteString(fmt.Sprintf("%s[i%d] = v%d;\n\n", valname, indent, indent))

			writeindent(&stream, indent-1)

			stream.WriteRune('}')

			return stream.String()

		}

		if isbytes {
			return fmt.Sprintf("%s = reader.readBytes();", valname)
		}

		var stream bytes.Buffer

		stream.WriteString(fmt.Sprintf("for(int i%d = 0; i%d < %s.length; i%d ++ ){\n\n", indent, indent, valname, indent))

		writeindent(&stream, indent)

		stream.WriteString(fmt.Sprintf("%s v%d = %s[i%d];\n\n", codegen.typeName(seq.Component), indent, valname, indent))

		writeindent(&stream, indent)

		stream.WriteString(codegen.readType(fmt.Sprintf("v%d", indent), seq.Component, indent+1))

		stream.WriteString("\n\n")

		writeindent(&stream, indent)

		stream.WriteString(fmt.Sprintf("%s[i%d] = v%d;\n\n", valname, indent, indent))

		writeindent(&stream, indent-1)

		stream.WriteRune('}')

		return stream.String()
	}

	gserrors.Panicf(nil, "typeName  error: unsupport type(%s)", codegen.typeName)

	return "unknown"
}
Ejemplo n.º 28
0
func (codegen *_CodeGen) readType(typeDecl ast.Type) string {
	switch typeDecl.(type) {
	case *ast.BuiltinType:
		builtinType := typeDecl.(*ast.BuiltinType)
		return readMapping[builtinType.Type]
	case *ast.TypeRef:
		typeRef := typeDecl.(*ast.TypeRef)

		return codegen.readType(typeRef.Ref)

	case *ast.Enum:
		prefix, name := codegen.typeRef(typeDecl.Package(), typeDecl.FullName())

		if prefix != "" {
			return prefix + ".Read" + name
		}

		return "Read" + name

	case *ast.Table:

		prefix, name := codegen.typeRef(typeDecl.Package(), typeDecl.FullName())

		if prefix != "" {
			return "" + prefix + ".Read" + name
		}

		return "Read" + name

	case *ast.Seq:
		seq := typeDecl.(*ast.Seq)

		var buff bytes.Buffer

		isbytes := false

		builtinType, ok := seq.Component.(*ast.BuiltinType)

		if ok && builtinType.Type == lexer.KeyByte {
			isbytes = true
		}

		if seq.Size != -1 {

			if isbytes {
				if err := codegen.tpl.ExecuteTemplate(&buff, "readByteArray", seq); err != nil {
					gserrors.Panicf(err, "exec template(readByteArray) for %s errir", seq)
				}
			} else {
				if err := codegen.tpl.ExecuteTemplate(&buff, "readArray", seq); err != nil {
					gserrors.Panicf(err, "exec template(readArray) for %s errir", seq)
				}
			}

		} else {

			if isbytes {
				if err := codegen.tpl.ExecuteTemplate(&buff, "readByteList", seq); err != nil {
					gserrors.Panicf(err, "exec template(readByteList) for %s errir", seq)
				}
			} else {
				if err := codegen.tpl.ExecuteTemplate(&buff, "readList", seq); err != nil {
					gserrors.Panicf(err, "exec template(readList) for %s errir", seq)
				}
			}

		}

		return buff.String()
	}

	gserrors.Panicf(nil, "typeName  error: unsupport type(%s)", typeDecl)

	return "unknown"
}
Ejemplo n.º 29
0
func (codegen *_CodeGen) writeType(valname string, typeDecl ast.Type, indent int) string {
	switch typeDecl.(type) {
	case *ast.BuiltinType:
		builtinType := typeDecl.(*ast.BuiltinType)
		return fmt.Sprintf("%s(%s);", writeMapping[builtinType.Type], valname)
	case *ast.TypeRef:
		typeRef := typeDecl.(*ast.TypeRef)

		return codegen.writeType(valname, typeRef.Ref, indent)

	case *ast.Enum, *ast.Table:
		return fmt.Sprintf("%s.marshal(writer);", valname)

	case *ast.Seq:
		seq := typeDecl.(*ast.Seq)

		isbytes := false

		builtinType, ok := seq.Component.(*ast.BuiltinType)

		if ok && builtinType.Type == lexer.KeyByte {
			isbytes = true
		}

		if seq.Size == -1 {

			if isbytes {

				return fmt.Sprintf("writer.writeBytes(%s);", valname)

			}

			var stream bytes.Buffer

			stream.WriteString(fmt.Sprintf("writer.writeUInt16((short)%s.length);\n\n", valname))

			writeindent(&stream, indent-1)

			stream.WriteString(fmt.Sprintf("for(%s v%d : %s){\n\n", codegen.typeName(seq.Component), indent, valname))

			writeindent(&stream, indent)

			stream.WriteString(codegen.writeType(fmt.Sprintf("v%d", indent), seq.Component, indent+1))

			stream.WriteString("\n\n")

			writeindent(&stream, indent-1)

			stream.WriteRune('}')

			return stream.String()

		}

		if isbytes {
			return fmt.Sprintf("writer.writeBytes(%s);", valname)
		}

		var stream bytes.Buffer

		stream.WriteString(fmt.Sprintf("writer.writeUInt16((short)%s.length);\n\n", valname))

		writeindent(&stream, indent-1)

		stream.WriteString(fmt.Sprintf("for(%s v%d : %s){\n\n", codegen.typeName(seq.Component), indent, valname))

		writeindent(&stream, indent)

		stream.WriteString(codegen.writeType(fmt.Sprintf("v%d", indent), seq.Component, indent+1))

		stream.WriteString("\n\n")

		writeindent(&stream, indent-1)

		stream.WriteRune('}')

		return stream.String()

	}

	gserrors.Panicf(nil, "writeType  error: unsupport type(%s)", typeDecl)

	return "unknown"
}
Ejemplo n.º 30
0
func (runner *_Runner) Run(main func(runner Runner)) {
	defer func() {
		if e := recover(); e != nil {
			runner.E("catch unknown exception\n\t%s", e)
		}

		runner.I("service stopped.")

		gslogger.Join()
	}()

	flag.Parse()

	configpath := *runner.flagString["config"]

	runner.D("config file path :%s", configpath)

	if configpath != "" {
		switch filepath.Ext(configpath) {
		case ".json":
			if err := gsconfig.LoadJSON(configpath); err != nil {
				gserrors.Panicf(ErrConfig, "load config file error :%s", configpath)
			}
		default:
			runner.W("can't load config file :%s", configpath)
		}
	}

	for k, v := range runner.flagString {
		gsconfig.Update(runner.fullname[k], *v)
	}
	for k, v := range runner.flagInt {
		gsconfig.Update(runner.fullname[k], *v)
	}
	for k, v := range runner.flagUint {
		gsconfig.Update(runner.fullname[k], *v)
	}
	for k, v := range runner.flagFloat32 {
		gsconfig.Update(runner.fullname[k], *v)
	}
	for k, v := range runner.flagFloat64 {
		gsconfig.Update(runner.fullname[k], *v)
	}
	for k, v := range runner.flagSecond {
		gsconfig.Update(runner.fullname[k], *v)
	}
	for k, v := range runner.flagMilliseconds {
		gsconfig.Update(runner.fullname[k], *v)
	}

	logroot := gsconfig.String("gsrunner.log", "")
	runner.D("log root path :%s", logroot)
	loglevel := gsconfig.String("gsrunner.log.level", "")
	runner.D("log level :%s", loglevel)
	registryFile := gsconfig.String("gsrunner.registry", "")
	runner.D("registry file:%s", registryFile)

	if logroot != "" {

		fullpath, _ := filepath.Abs(logroot)

		dir := filepath.Dir(fullpath)

		name := filepath.Base(fullpath)

		gslogger.SetLogDir(dir)

		gslogger.NewSink(gslogger.NewFilelog("gschat-mailhub", name, gsconfig.Int64("gschat.mailhub.log.maxsize", 0)))
	}

	if loglevel != "" {
		gslogger.NewFlags(gslogger.ParseLevel(loglevel))
	}

	if registryFile != "" {

		runner.I("load gsrpc services registry file :%s", registryFile)

		file, err := os.Open(registryFile)

		if err != nil {
			gserrors.Panicf(err, "open registry file error :%s", registryFile)
		}

		gorpc.RegistryLoad(file, registryFile)

		runner.I("load gsrpc services registry file :%s -- success", registryFile)
	}

	runner.I("service started.")

	main(runner)
}