Beispiel #1
0
func (n *node) defineField(w io.Writer, f caps.Field, x *bam.Extractor) {
	t := f.Slot().Type()

	if t.Which() == caps.TYPE_INTERFACE {
		return
	}

	var fname string

	if an := nameAnnotation(f.Annotations()); an != "" {
		fname = an
	} else {
		fname = f.Name()
	}

	fname = strings.Title(fname)

	var g, s bytes.Buffer

	if f.DiscriminantValue() != 0xFFFF {
		if t.Which() == caps.TYPE_VOID {
			x.SetUnionStruct()
			w.Write(s.Bytes())
			return
		}
	} else if t.Which() == caps.TYPE_VOID {
		return
	}

	customtype := ""
	for _, a := range f.Annotations().ToArray() {
		if a.Id() == C.Doc {
			fmt.Fprintf(&g, "// %s\n", a.Value().Text())
		}
		if a.Id() == C.Customtype {
			customtype = a.Value().Text()
			if i := strings.LastIndex(customtype, "."); i != -1 {
				g_imported[customtype[:i]] = true
			}
		}
	}

	if len(customtype) != 0 {
		log.Println("CUSTOM TYPE:", customtype)
	}

	fmt.Fprintf(&s, "%s ", fname)

	typeName := GoTypeName(n, f.Slot(), customtype)
	fmt.Fprintf(&s, "%s", typeName)

	fld := &ast.Field{}
	goseq := strings.SplitAfter(typeName, "[]")
	typePrefix := ""
	if len(goseq) == 2 {
		typeName = goseq[1]
		typePrefix = goseq[0]
	}

	x.GenerateStructField(fname, typePrefix, typeName, fld, t.Which() == caps.TYPE_LIST, fld.Tag, false, goseq)

	ans := f.Annotations()
	n.processAnnotations(&s, f, t.Which(), ans)

	fmt.Fprintf(&s, "\n")

	w.Write(g.Bytes())
	w.Write(s.Bytes())
}