Example #1
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"
}
Example #2
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())
	}

}
Example #3
0
// NewCodeGen .
func NewCodeGen(rootpath string, skips []string) (gslang.Visitor, error) {

	codeGen := &_CodeGen{
		Log:      gslogger.Get("gen4go"),
		rootpath: rootpath,
	}

	for _, skip := range skips {
		exp, err := regexp.Compile(skip)

		if err != nil {
			return nil, gserrors.Newf(err, "invalid skip regex string :%s", skip)
		}

		codeGen.skips = append(codeGen.skips, exp)
	}

	funcs := template.FuncMap{
		"exception": exception,
		"title":     strings.Title,
		"tableName": func(typeDecl ast.Type) string {
			if gslang.IsException(typeDecl) {
				return exception(strings.Title(typeDecl.Name()))
			}

			return strings.Title(typeDecl.Name())
		},
		"methodName":  methodName,
		"fieldName":   fieldname,
		"enumFields":  codeGen.enumFields,
		"notVoid":     gslang.NotVoid,
		"isPOD":       gslang.IsPOD,
		"isAsync":     gslang.IsAsync,
		"isException": gslang.IsException,
		"enumSize":    gslang.EnumSize,
		"enumType": func(typeDecl ast.Type) string {
			return builtin[gslang.EnumType(typeDecl)]
		},
		"builtin":         gslang.IsBuiltin,
		"typeName":        codeGen.typeName,
		"objTypeName":     codeGen.objTypeName,
		"defaultVal":      codeGen.defaultVal,
		"readType":        codeGen.readType,
		"writeType":       codeGen.writeType,
		"params":          codeGen.params,
		"returnParam":     codeGen.returnParam,
		"callArgs":        codeGen.callArgs,
		"returnArgs":      codeGen.returnArgs,
		"marshalField":    codeGen.marshalfield,
		"unmarshalField":  codeGen.unmarshalfield,
		"unmarshalParam":  codeGen.unmarshalParam,
		"methodcall":      codeGen.methodcall,
		"marshalParam":    codeGen.marshalParam,
		"marshalReturn":   codeGen.marshalReturn,
		"methodRPC":       codeGen.methodRPC,
		"marshalParams":   codeGen.marshalParams,
		"callback":        codeGen.callback,
		"unmarshalReturn": codeGen.unmarshalReturn,
		"constructor":     codeGen.constructor,
		"tagValue":        codeGen.tagValue,
	}

	tpl, err := template.New("t4java").Funcs(funcs).Parse(t4java)

	if err != nil {
		return nil, err
	}

	codeGen.tpl = tpl

	return codeGen, nil
}