Example #1
0
func nameAnnotation(annotations caps.Annotation_List) string {
	for _, a := range annotations.ToArray() {
		if a.Id() == C.Name {
			if name := a.Value().Text(); name != "" {
				return name
			}
		}
	}
	return ""
}
Example #2
0
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, " "))
	}
}