Skip to content

Commit

Permalink
Chore: support JSON configuration with comments and trailing commas
Browse files Browse the repository at this point in the history
  • Loading branch information
Loyalsoldier committed Oct 23, 2024
1 parent e6ee2bb commit 3d3d5e5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
module github.com/Loyalsoldier/geoip

go 1.22
go 1.23

toolchain go1.22.6
toolchain go1.23.2

require (
github.com/klauspost/compress v1.17.10
github.com/maxmind/mmdbwriter v1.0.0
github.com/oschwald/maxminddb-golang v1.13.1
github.com/sagernet/sing-box v1.9.7
github.com/spf13/cobra v1.8.1
github.com/tailscale/hujson v0.0.0-20241010212012-29efb4a0184b
github.com/tidwall/gjson v1.18.0
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
google.golang.org/protobuf v1.35.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tailscale/hujson v0.0.0-20241010212012-29efb4a0184b h1:MNaGusDfB1qxEsl6iVb33Gbe777IKzPP5PDta0xGC8M=
github.com/tailscale/hujson v0.0.0-20241010212012-29efb4a0184b/go.mod h1:EbW0wDK/qEUYI0A5bqq0C2kF8JTQwWONmGDBbzsxxHo=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
Expand Down
8 changes: 8 additions & 0 deletions lib/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"os"
"strings"

"github.com/tailscale/hujson"
)

type Instance struct {
Expand Down Expand Up @@ -34,6 +36,9 @@ func (i *Instance) Init(configFile string) error {
return err
}

// Support JSON with comments and trailing commas
content, _ = hujson.Standardize(content)

if err := json.Unmarshal(content, &i.config); err != nil {
return err
}
Expand All @@ -50,6 +55,9 @@ func (i *Instance) Init(configFile string) error {
}

func (i *Instance) InitFromBytes(content []byte) error {
// Support JSON with comments and trailing commas
content, _ = hujson.Standardize(content)

if err := json.Unmarshal(content, &i.config); err != nil {
return err
}
Expand Down

0 comments on commit 3d3d5e5

Please sign in to comment.