parser

package
v0.0.0-...-8ccf8f9 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

README

Generic Parser for ALB Annotations

Usage : Add support to a new annotation

Example Annotation
ingress.bluemix.net/proxy-connect-timeout : "serviceName=tea-svc timeout=60s"

The above annotation will apply 60s proxy connect timeout to tea-svc location

 ingress.bluemix.net/proxy-connect-timeout : "timeout=60s"
 

The above annotation will apply 60s to all locations

  ingress.bluemix.net/proxy-connect-timeout : "60s"
 

Just like last example annotation will apply 60s to all locations. This is an example of keyless entry

Configuring Annotation in annotations.json (/nginx-controller/parser/annotations.json)

Following are the characteristics of the example annotation

  1. 2 fields in which timeout is a mandatory field

  2. serviceName is an optional field - its a string

  3. If no fields provided it would be taken as timeout

  4. timeout(60s) has to have a valid unit ("s") as suffix and prefix should be an integer (60)

In annotations.json the above can be configured trivially as

   {
     "label":"ingress.bluemix.net/proxy-connect-timeout",
     "mandatoryFields":[
        {
           "name":"timeout",
           "type":"timeout"
        }
    ],
     "atleastoneFields":[],
     "optionalFields":[
        {
           "name":"serviceName",
           "type":"string"
        }],
     "keyLessEntry" : "True"
}
Fields supported and Custom fields (refer /nginx-controller/parser/fieldtypes.go)

In the above example - timeout is a custom field which we need to be added to fieldtypes.go.

step 1: Add field to fieldTypeToParserMap as shown below

var fieldTypeToParserMap = map[string]ParseMethodDef{
	"int":     parseInteger,
	"string":  parseString,
	"bool":    parseBoolean,
	"key":     parseKey,
	"rate":    parseRate,
	"timeout": parseTimeout,
}

step 2: Add your field parser method

The field parser method should be an extension of Field Parser Defintion

type ParseMethodDef func(valueInput string) (value interface{}, err error)

in our case timeout parser method signature should be as given below.

func parseTimeout(timeoutPart string) (value interface{}, err error)

Please note following

  1. We should go for a new custom field only if existing fields available in fieldstypes.go are not suiting to our purpose and field validation

  2. All the field level validations can be added here

For eg: if you want to add a new field called "port" all the port related validations can be added in "parsePort" method

Parser will execute those validations in all annotations "port" field is used

How to call parser and Use Model
annotationModel, err := parser.ParseInputForAnnotation("ingress.bluemix.net/proxy-connect-timeout", annotationStringFromIng)

If there is any error in the parsing - an error object will be returned which should be checked before starting use the annotationModel object. In configurator getAnnotationModel method can be used for the same purpose

  1. annotationModel is an object of struct ParsedValidatedAnnotation ( refer model.go)

the model has list of Entry objects in it

ingress.bluemix.net/proxy-connect-timeout : "serviceName=tea-svc timeout=60s;serviceName=coffee-svc timeout=60s"

In this case it will have 2 entries.Entry1 is serviceName=tea-svc timeout=60s Entry 2 is serviceName=coffee-svc timeout=60s

  1. Entries can be traversed as a normal list
 for _, entry := range annotationModel.Entries 

Each entry has all the fields that represents a single sentence

Following are the methods in Entry that can be used to fetch values

  • entry.GetAsString("serviceName") will return you the serviceName value for that entry.Please make sure that this is used against string type field. You can use entry.GetAsStrings("serviceName") if you are expecting serviceName=tea-svc,coffee-svc format

  • entry.GetAsInt("conn") will return you the conn value for that entry. Please make sure that this is used against int type fields

  • entry.Exists("serviceName") checks whether serviceName is provided in the entry

  • entry.GetAsValueUnitString("rate") - this will return fields like rate(5r/s), timeout(60s) etc which have a value and unit in it .Please see the fieldtypes.go for defenition of parseRate and parseTimeout methods

We can add more methods to model (like GetAsFloats etc..) based on the future requirements for new types of fields if any.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseTimeout

func ParseTimeout(timeoutPart string) (value interface{}, err error)

ParseTimeout converts any timeout input with "s" or "m " into seconds

func ParseTimeoutToSeconds

func ParseTimeoutToSeconds(timeoutPart string) (numSeconds int, err error)

ParseTimeoutToSeconds converts any timeout input with "s" or "m " into seconds

func Prepare

func Prepare(filenames ...string)

Prepare ...

func TimeoutParser

func TimeoutParser(timeoutPart string, allowZero bool, allowedUnits []string) (value interface{}, err error)

TimeoutParser parses a timeout value (ie 10s) and returns an interface {10, s}

Types

type AnnotationDefintion

type AnnotationDefintion struct {
	// contains filtered or unexported fields
}

AnnotationDefintion ...

type AnnotationGrammer

type AnnotationGrammer struct {
	Definitions []struct {
		Label           string `json:"label"`
		MandatoryFields []struct {
			Name string `json:"name"`
			Type string `json:"type"`
		} `json:"mandatoryFields"`
		AtleastoneFields []struct {
			Name string `json:"name"`
			Type string `json:"type"`
		} `json:"atleastoneFields"`
		OptionalFields []struct {
			Name string `json:"name"`
			Type string `json:"type"`
		} `json:"optionalFields"`
		StrictOrdering string `json:"strictOrdering,omitempty"`
		KeyLessEntry   string `json:"keyLessEntry,omitempty"`
	} `json:"definition"`
}

AnnotationGrammer ...

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

Entry ... Output model

func NewEntry

func NewEntry() Entry

NewEntry ... Constructor Entry

func NewKeylessEntry

func NewKeylessEntry(keylessValue string) Entry

NewKeylessEntry ... Constructor For KeylessEntry

func (Entry) Exists

func (entry Entry) Exists(key string) (isPresent bool)

Exists check if the key exists for a given entry

func (Entry) Get

func (entry Entry) Get(key string) (values []interface{}, exists bool)

Get ...

func (Entry) GetAsBool

func (entry Entry) GetAsBool(key string) (boolvalue bool, exists bool)

GetAsBool support for boolean values for example : proxy-buffering : True or false

func (Entry) GetAsBools

func (entry Entry) GetAsBools(key string) (boolvalues []bool, exists bool)

GetAsBools support for multiple boolean values

func (Entry) GetAsInt

func (entry Entry) GetAsInt(key string) (intvalue int, exists bool)

GetAsInt Get the values of single Integer value for given fieldName for a given entry

func (Entry) GetAsInts

func (entry Entry) GetAsInts(key string) (intvalues []int, exists bool)

GetAsInts Get the values of multiple Integer value for given fieldName for a given entry

func (Entry) GetAsString

func (entry Entry) GetAsString(key string) (strvalue string, exists bool)

GetAsString Get the values of single string value for given fieldName for a given entry

func (Entry) GetAsStrings

func (entry Entry) GetAsStrings(key string) (strvalues []string, exists bool)

GetAsStrings Get the values of mutiple string vlaues for a given filedname for a given entry

func (Entry) GetAsValueUnitString

func (entry Entry) GetAsValueUnitString(key string) (valueUnit string, exists bool)

GetAsValueUnitString Get the values of single string value having unit for given fieldName for a given entry ex : timeout,rate

func (Entry) GetAsValueUnitStringArray

func (entry Entry) GetAsValueUnitStringArray(key string) (strvalues [2]string, exists bool)

GetAsValueUnitStringArray Get the first array values of the multiple string having units of key

func (Entry) GetAsValueUnitStringArrays

func (entry Entry) GetAsValueUnitStringArrays(key string) (strvalues [][2]string, exists bool)

GetAsValueUnitStringArrays Get all the values of the strings values for the given field name

func (Entry) GetKeylessValue

func (entry Entry) GetKeylessValue() (values []interface{}, exists bool)

GetKeylessValue gets the keyless value of a given entry

func (Entry) GetKeylessValueAsBool

func (entry Entry) GetKeylessValueAsBool(fieldType string) (strvalue bool, exists bool)

GetKeylessValueAsBool Support for getting boolean value for keyless value

func (Entry) GetKeylessValueAsString

func (entry Entry) GetKeylessValueAsString(fieldType string) (strvalue string, exists bool)

GetKeylessValueAsString Support for getting string values for a given fieldType for example : "cafe.example.com"

func (Entry) GetKeylessValueAsValueUnitString

func (entry Entry) GetKeylessValueAsValueUnitString(fieldType string) (strvalue string, exists bool)

GetKeylessValueAsValueUnitString Support for getting string unit values for a given fieldType for example : 10ms , 10r/s

func (Entry) Keyless

func (entry Entry) Keyless() (isKeyless bool)

Keyless checks if the entry is keyless

func (Entry) Put

func (entry Entry) Put(key string, value interface{})

Put ...

type ParseMethodDef

type ParseMethodDef func(valueInput string) (value interface{}, err error)

ParseMethodDef - Field Parser Defintion

type ParsedValidatedAnnotation

type ParsedValidatedAnnotation struct {
	Entries []Entry
	// contains filtered or unexported fields
}

ParsedValidatedAnnotation ...

func ParseInputForAnnotation

func ParseInputForAnnotation(label string, input string) (parsedOutput ParsedValidatedAnnotation, err error)

ParseInputForAnnotation ...

func (ParsedValidatedAnnotation) GetEntriesWithFieldValueInt

func (parsedValidatedAnnotation ParsedValidatedAnnotation) GetEntriesWithFieldValueInt(fieldName string, fieldValue int) (selectedEntries []Entry, exists bool)

GetEntriesWithFieldValueInt gets the entries which containing integer values

func (ParsedValidatedAnnotation) GetEntriesWithFieldValueString

func (parsedValidatedAnnotation ParsedValidatedAnnotation) GetEntriesWithFieldValueString(fieldName string, fieldValue string) (selectedEntries []Entry, exists bool)

GetEntriesWithFieldValueString support only string values for example : serviceName

func (ParsedValidatedAnnotation) GetEntriesWithFieldValueUnitString

func (parsedValidatedAnnotation ParsedValidatedAnnotation) GetEntriesWithFieldValueUnitString(fieldName string, fieldValue string) (selectedEntries []Entry, exists bool)

GetEntriesWithFieldValueUnitString support only string values having units for example : timeout , rate

func (ParsedValidatedAnnotation) GetEntriesWithSingleField

func (parsedValidatedAnnotation ParsedValidatedAnnotation) GetEntriesWithSingleField(fieldName string) (selectedEntries []Entry, exists bool)

GetEntriesWithSingleField gets entries with single string field for example : serviceName

Jump to

Keyboard shortcuts

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