annotation

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2021 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Annotation Go Report Card Coverage Status Build Status

Annotation is a go package that allows parsing and read Java Style annotation strings e.x @SomeAnnotation(param="value"), this package is used by go-service to read and parse annotations in comments.

Install

Using go get

go get -u github.com/go-services/annotation

If you are using dep

dep ensure --add github.com/go-services/annotation

Usage

The package can only be used for string that only contain the annotation, it can not find the annotation in an arbitrary string.

Example

package main

import (
	"github.com/go-services/annotation"
	"fmt"
)

func main() {
	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
}

Definitions

Annotation also provides a way to check if the annotation has the correct parameters, name.

ParameterDefinition

type ParameterDefinition struct {
	// name the parameter name
	name     string
	
	// required tells if the parameter is required
	required bool
	
	// tp shows the required type of the annotation
	tp       ValueType
}

Definition

// Definition describes the Annotation definition.
type Definition struct {
	// Name is the Name of the Annotation e.x Hello for // @Hello().
	name string

	// should the definition allow unknown parameters
	allowUnknownParameters bool

	// parameters has a list of parameter definitions
	parameters []ParameterDefinition
}

Example

package main

import (
	"github.com/go-services/annotation"
	"fmt"
)

func main() {
    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

}

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 Parse

func Parse(s string) (*Annotation, error)

Parse finds an ann in a string.

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 added in v0.1.3

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

type Value interface {
	String() string
	Int() int
	Float() float64
	Bool() bool
	Type() ValueType
}

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"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址