thriftlint/annotations.go

29 lines
779 B
Go
Raw Permalink Normal View History

2017-01-11 04:36:58 +00:00
package thriftlint
import (
"reflect"
"github.com/alecthomas/go-thrift/parser"
)
// Annotation returns the annotation value associated with "key" from the .Annotations field of a
// go-thrift AST node.
//
// This will panic if node is not a struct with an Annotations field of the correct type.
func Annotation(node interface{}, key, dflt string) string {
annotations := reflect.Indirect(reflect.ValueOf(node)).
FieldByName("Annotations").
Interface().([]*parser.Annotation)
for _, annotation := range annotations {
if annotation.Name == key {
return annotation.Value
}
}
return dflt
}
// AnnotationExists checks if an annotation is present at all.
func AnnotationExists(node interface{}, key string) bool {
return Annotation(node, key, "\000") != "\000"
}