Documentation ¶
Overview ¶
Package annotation is used to parse and check java style annotation (e.x @SomeAnnotation(param="value"))
The package can only be used for string that only contain the annotation, it can not find the annotation in an arbitrary string.
Usage
annotationString := `@Annotation( stringParam='String Value', someInt=2, someBool=true, someFloat=2.5 )` ann, _ := annotation.Parse(annotationString) fmt.Printf("Annotation Name: %s\n", ann.Name) // Annotation Name: Annotation fmt.Printf("Annotation stringParam = %s\n", ann.Get("stringParam").String()) // Annotation stringParam = String Value fmt.Printf("Annotation someInt = %d\n", ann.Get("someInt").Int()) // Annotation someInt = 2 fmt.Printf("Annotation someBool = %t\n", ann.Get("someBool").Bool()) // Annotation someBool = true fmt.Printf("Annotation someFloat = %.4f\n", ann.Get("someFloat").Float()) // Annotation someInt = 2.5000
You can check the annotation using definitions
annotationString := `@Annotation( stringParam='String Value', someInt=2, someBool=true, someFloat=2.5 )` ann, _ := annotation.Parse(annotationString) stringParam := annotation.NewParameterDefinition("stringParam", true, annotation.STRING) someInt := annotation.NewParameterDefinition("someInt", true, annotation.INT) someBool := annotation.NewParameterDefinition("someBool", true, annotation.BOOL) someFloat := annotation.NewParameterDefinition("someFloat", true, annotation.FLOAT) definition := annotation.NewDefinition( "Annotation", false, stringParam, someInt, someBool, someFloat, ) err := definition.Check(*ann) fmt.Println(err) // <nil> annotationMissingParam := "@Annotation(someInt=2, someBool=true, someFloat=2.5)" annWrong, _ := annotation.Parse(annotationMissingParam) err = definition.Check(*annWrong) fmt.Println(err) // the `stringParam` parameter is required for @Annotation() Annotation
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Annotation ¶
type Annotation struct { Name string // contains filtered or unexported fields }
Annotation is the parsed annotation
func NewAnnotation ¶
func NewAnnotation(name string) Annotation
NewAnnotation creates a new Annotation.
func (*Annotation) Get ¶
func (a *Annotation) Get(name string) Value
Get returns the parameter value by name if that parameter does not exist it will return an empty value of type 'UNKNOWN'
func (*Annotation) Set ¶
func (a *Annotation) Set(name string, value attrValue)
func (*Annotation) String ¶
func (a *Annotation) String() string
String returns the annotation string
type Definition ¶
type Definition struct {
// contains filtered or unexported fields
}
Definition describes the Annotation definition.
func NewDefinition ¶
func NewDefinition(name string, allowUnknownParameters bool, parameters ...ParameterDefinition) Definition
NewDefinition creates a new Annotation Definition.
func (*Definition) Check ¶
func (d *Definition) Check(annotation Annotation) error
Check checks if the annotation matches the definition
type ParameterDefinition ¶
type ParameterDefinition struct {
// contains filtered or unexported fields
}
ParameterDefinition describes the parameter definition
func NewParameterDefinition ¶
func NewParameterDefinition(name string, required bool, parameterType ValueType) ParameterDefinition
NewParameterDefinition returns a new parameter definition
type Value ¶
Value is the interface that wraps the annotation parameter tha parameter can be represented in all of the below types, if the parameter value is not convertible to a type the value will be the zero value of the type
type ValueType ¶
type ValueType string
ValueType is a string that tells the type of the parsed parameter
const ( // STRING represents a string type parameter STRING ValueType = "string" // INT represents an int type parameter INT ValueType = "int" // FLOAT represents a float type parameter FLOAT ValueType = "float" // BOOL represents a bool type parameter BOOL ValueType = "bool" // UNKNOWN represents an unknown type parameter (usually if the parameter does not exist) UNKNOWN ValueType = "unknown" )