Skip to content

Commit

Permalink
Again allow comment header in JSON for NSX
Browse files Browse the repository at this point in the history
  • Loading branch information
hknutzen committed Feb 15, 2024
1 parent 23bdd70 commit 63eb563
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
17 changes: 16 additions & 1 deletion go/pkg/nsx/parse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nsx

import (
"bytes"
"encoding/json"
"fmt"

Expand Down Expand Up @@ -112,7 +113,7 @@ func (s *State) ParseConfig(data []byte, fName string) (
if len(data) == 0 {
return config, nil
}
err := json.Unmarshal(data, config)
err := json.Unmarshal(removeHeader(data), config)
if err != nil {
return nil, err
}
Expand All @@ -136,3 +137,17 @@ func checkConfigValidity(c *NsxConfig) error {
}
return nil
}

func removeHeader(data []byte) []byte {
for {
if bytes.HasPrefix(data, []byte("#")) {
i := bytes.IndexByte(data, byte('\n'))
if i == -1 {
return data[len(data):]
}
data = data[i+1:]
} else {
return data
}
}
}
25 changes: 22 additions & 3 deletions go/testdata/nsx.t
Original file line number Diff line number Diff line change
Expand Up @@ -833,15 +833,34 @@ ERROR>>> While reading router: invalid character 'i' looking for beginning of ob
############################################################
=TITLE=Invalid JSON from device
=DEVICE=
# Some comment
[[one_rule]]
#Good

#Bad
=NETSPOC=
Generated by Netspoc devel
[[one_rule]]
=ERROR=
ERROR>>> While reading device: invalid character '#' looking for beginning of value
=END=

############################################################
=TITLE=Remove comment header from file
=DEVICE=
#Some comment
# More
# lines follow
[[one_rule]]
=NETSPOC=
[[one_rule]]
=OUTPUT=NONE

############################################################
=TITLE=File with only comment header
=DEVICE=# Some comment
=NETSPOC={}
=ERROR=
ERROR>>> While reading device: unexpected end of JSON input
=END=

############################################################
=TITLE=Patch existing Service
# Remove source_ports from service
Expand Down

0 comments on commit 63eb563

Please sign in to comment.