示例#1
0
文件: capnpc-pgo.go 项目: tpukep/caps
func (n *node) processAnnotations(w io.Writer, f caps.Field, t caps.Type_Which, ans caps.Annotation_List) {
	annotations := make(map[uint64]caps.Annotation)

	for _, a := range ans.ToArray() {
		annotations[a.Id()] = a
	}

	req, required := annotations[caps.FieldRequired]
	opt, optional := annotations[caps.FieldOptional]
	_, ignored := annotations[caps.FieldIgnored]

	assert(!(required && ignored), "Field annnotations 'required' and 'ignored' are incompatible.")
	assert(!(required && optional), "Annnotations 'required' and 'optional' are incompatible")
	assert(!(optional && ignored), "Annnotations 'optional' and 'ignored' are incompatible")

	var tags []string
	var checkTags []string

	// Codecs Tags
	if ignored {
		if _, found := n.codecs[caps.CodecJson]; found {
			tags = append(tags, fmt.Sprintf("json:\"-\""))
		}
		if _, found := n.codecs[caps.CodecMsgp]; found {
			tags = append(tags, fmt.Sprintf("msg:\"-\""))
		}

		checkTags = append(checkTags, "-")
	} else if optional {
		if _, found := n.codecs[caps.CodecJson]; found {
			tags = append(tags, fmt.Sprintf("json:\"%s,omitempty\"", opt.Value().Text()))
		}
		if _, found := n.codecs[caps.CodecMsgp]; found {
			tags = append(tags, fmt.Sprintf("msg:\"%s\"", opt.Value().Text()))
		}

		checkTags = append(checkTags, "omitempty")
	} else if required {
		if _, found := n.codecs[caps.CodecJson]; found {
			tags = append(tags, fmt.Sprintf("json:\"%s\"", req.Value().Text()))
		}
		if _, found := n.codecs[caps.CodecMsgp]; found {
			tags = append(tags, fmt.Sprintf("msg:\"%s\"", req.Value().Text()))
		}

		checkTags = append(checkTags, "required")
	} else {
		if _, found := n.codecs[caps.CodecJson]; found {
			tags = append(tags, fmt.Sprintf("json:\"%s\"", f.Name()))
		}
		if _, found := n.codecs[caps.CodecMsgp]; found {
			tags = append(tags, fmt.Sprintf("msg:\"%s\"", f.Name()))
		}
	}

	// Check annotation
	if exp, found := annotations[caps.CheckValue]; found {
		checkTags = append(checkTags, exp.Value().Text())
	}

	if len(checkTags) != 0 {
		tags = append(tags, fmt.Sprintf("validate:\"%s\"", strings.Join(checkTags, ",")))
	}

	if len(tags) != 0 {
		fmt.Fprintf(w, "`%s`", strings.Join(tags, " "))
	}
}
示例#2
0
文件: capnpc-pgo.go 项目: tpukep/caps
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())
}