| 1 | /* |
| 2 | * This file is part of the go-nft project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * Copyright 2021 Red Hat, Inc. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | package nft |
| 21 | |
| 22 | import ( |
| 23 | "github.com/networkplumbing/go-nft/nft/schema" |
| 24 | ) |
| 25 | |
| 26 | // NewRule returns a new schema rule structure. |
| 27 | func NewRule(table *schema.Table, chain *schema.Chain, expr []schema.Statement, handle *int, index *int, comment string) *schema.Rule { |
| 28 | c := &schema.Rule{ |
| 29 | Family: table.Family, |
| 30 | Table: table.Name, |
| 31 | Chain: chain.Name, |
| 32 | Expr: expr, |
| 33 | Handle: handle, |
| 34 | Index: index, |
| 35 | Comment: comment, |
| 36 | } |
| 37 | |
| 38 | return c |
| 39 | } |
| 40 | |
| 41 | type RuleIndex int |
| 42 | |
| 43 | // NewRuleIndex returns a rule index object which acts as an iterator. |
| 44 | // When multiple rules are added to a chain, index allows to define an order between them. |
| 45 | // The first rule which is added to a chain should have no index (it is assigned index 0), |
| 46 | // following rules should have the index set, referencing after/before which rule the new one is to be added/inserted. |
| 47 | func NewRuleIndex() *RuleIndex { |
| 48 | var index RuleIndex = -1 |
| 49 | return &index |
| 50 | } |
| 51 | |
| 52 | // Next returns the next iteration value as an integer pointer. |
| 53 | // When first time called, it returns the value 0. |
| 54 | func (i *RuleIndex) Next() *int { |
| 55 | *i++ |
| 56 | var index = int(*i) |
| 57 | return &index |
| 58 | } |