From 218bca458e1f22df7117ef6240d2b38c7bcb1028 Mon Sep 17 00:00:00 2001 From: Garfield Lee Freeman Date: Fri, 29 Mar 2024 14:05:32 +0100 Subject: [PATCH] adding positioning support --- rule/position.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 rule/position.go diff --git a/rule/position.go b/rule/position.go new file mode 100644 index 0000000..a22ea3d --- /dev/null +++ b/rule/position.go @@ -0,0 +1,70 @@ +package rule + +import ( + "fmt" + + "github.com/PaloAltoNetworks/pango/errors" +) + +type Position struct { + First *bool `json:"first,omitempty"` + Last *bool `json:"last,omitempty"` + SomewhereBefore *string `json:"somewhere_before,omitempty"` + DirectlyBefore *string `json:"directly_before,omitempty"` + SomewhereAfter *string `json:"somewhere_after,omitempty"` + DirectlyAfter *string `json:"directly_after,omitempty"` +} + +func (o *Position) IsValid(removeEverythingElse bool) error { + count := 0 + + if o.First != nil && *o.First { + count++ + } + + if o.Last != nil && *o.Last { + count++ + } + + if o.SomewhereBefore != nil { + if removeEverythingElse { + return errors.RelativePositionWithRemoveEverythingElseError + } + if *o.SomewhereBefore != "" { + count++ + } + } + + if o.DirectlyBefore != nil { + if removeEverythingElse { + return errors.RelativePositionWithRemoveEverythingElseError + } + if *o.DirectlyBefore != "" { + count++ + } + } + + if o.SomewhereAfter != nil { + if removeEverythingElse { + return errors.RelativePositionWithRemoveEverythingElseError + } + if *o.SomewhereAfter != "" { + count++ + } + } + + if o.DirectlyAfter != nil { + if removeEverythingElse { + return errors.RelativePositionWithRemoveEverythingElseError + } + if *o.DirectlyAfter != "" { + count++ + } + } + + if count > 1 { + return fmt.Errorf("multiple positions specified: only one should be specified") + } + + return nil +}