Debian Codegraph @

nft/schema/schema.go

raw symbols SCIP
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 schema
21
22 import (
23 "encoding/json"
24 )
25
26 type Root struct {
27 Nftables []Nftable `json:"nftables"`
28 }
29
30 const ruleSetKey = "ruleset"
31
32 type Objects struct {
33 Table *Table `json:"table,omitempty"`
34 Chain *Chain `json:"chain,omitempty"`
35 Rule *Rule `json:"rule,omitempty"`
36 Ruleset bool `json:"-"`
37 }
38
39 func (o Objects) MarshalJSON() ([]byte, error) {
40 type _Objects Objects
41 objects := _Objects(o)
42
43 data, err := json.Marshal(objects)
44 if err != nil {
45 return nil, err
46 }
47
48 if o.Ruleset {
49 // Convert to a dynamic structure
50 var dynamicStructure map[string]json.RawMessage
51 if err := json.Unmarshal(data, &dynamicStructure); err != nil {
52 return nil, err
53 }
54 dynamicStructure[ruleSetKey] = nil
55 data, err = json.Marshal(dynamicStructure)
56 if err != nil {
57 return nil, err
58 }
59 }
60
61 return data, nil
62 }
63
64 type Nftable struct {
65 Table *Table `json:"table,omitempty"`
66 Chain *Chain `json:"chain,omitempty"`
67 Rule *Rule `json:"rule,omitempty"`
68
69 Add *Objects `json:"add,omitempty"`
70 Delete *Objects `json:"delete,omitempty"`
71 Flush *Objects `json:"flush,omitempty"`
72
73 Metainfo *Metainfo `json:"metainfo,omitempty"`
74 }
75
76 type Metainfo struct {
77 Version string `json:"version"`
78 ReleaseName string `json:"release_name"`
79 JsonSchemaVersion int `json:"json_schema_version"`
80 }