Exemplo n.º 1
0
func (n *node) defineStructFields(w io.Writer, x *bam.Extractor) {
	assert(n.Which() == caps.NODE_STRUCT, "invalid struct node")

	for _, f := range n.codeOrderFields() {
		switch f.Which() {
		case caps.FIELD_SLOT:
			n.defineField(w, f, x)
		case caps.FIELD_GROUP:
			g := findNode(f.Group().TypeId())
			fname := f.Name()
			if an := nameAnnotation(f.Annotations()); an != "" {
				fname = an
			}
			fname = strings.Title(fname)

			typeName := ""
			fld := &ast.Field{}
			x.GenerateStructField(fname, "", typeName, fld, false, fld.Tag, true, []string{typeName})

			fmt.Fprintf(w, "%s struct {\n", fname)
			g.defineStructFields(w, x)

			fmt.Fprintf(w, "}\n")
		}
	}
}
Exemplo n.º 2
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())
}