Example #1
0
func (c *astConverter) maybeReplace(node ast.Node, ann *annotations.Annotation, replace func(e ast.Expr)) bool {
	if replace == nil {
		return false
	}

	if typ, ok := ann.Type(); ok {
		e, err := parser.ParseExpr(typ)
		if err == nil {
			replace(e)
			return true
		}
	}

	n := reflect.ValueOf(node)

	if n.Elem().Type().Kind() != reflect.Struct {
		return false
	}

	doc := n.Elem().FieldByName("Doc")
	if !doc.IsValid() {
		return false
	}

	cg := doc.Interface().(*ast.CommentGroup)
	if cg == nil {
		return false
	}

	for _, l := range cg.List {
		s := l.Text
		s = strings.TrimPrefix(s, "//")
		s = strings.TrimPrefix(s, "/*")
		s = strings.TrimSpace(s)

		if !strings.HasPrefix(s, "For SGo: ") {
			continue
		}

		ann := s[len("For SGo: "):]
		e, err := parser.ParseExpr(ann)
		if err != nil {
			return false
		}

		replace(e)
		return true
	}

	return false
}