Skip to content

Commit

Permalink
replace DisposaBoy/JsonConfigReader with tinode/jsonco
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Mar 31, 2020
1 parent 4f376a0 commit 54dfd19
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 53 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.14

require (
firebase.google.com/go v3.12.0+incompatible
github.com/DisposaBoy/JsonConfigReader v0.0.0-20171218180944-5ea4d0ddac55
github.com/aws/aws-sdk-go v1.29.29
github.com/bitly/go-hostpool v0.1.0 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
Expand All @@ -16,6 +15,7 @@ require (
github.com/jmoiron/sqlx v1.2.0
github.com/prometheus/client_golang v1.5.1
github.com/prometheus/common v0.9.1
github.com/tinode/jsonco v1.0.0
github.com/tinode/snowflake v1.0.0
go.mongodb.org/mongo-driver v1.3.1
golang.org/x/crypto v0.0.0-20200320181102-891825fb96df
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSR
firebase.google.com/go v3.12.0+incompatible h1:q70KCp/J0oOL8kJ8oV2j3646kV4TB8Y5IvxXC0WT1bo=
firebase.google.com/go v3.12.0+incompatible/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIwjt8toICdV5Wh9ptHs=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DisposaBoy/JsonConfigReader v0.0.0-20171218180944-5ea4d0ddac55 h1:jbGlDKdzAZ92NzK65hUP98ri0/r50vVVvmZsFP/nIqo=
github.com/DisposaBoy/JsonConfigReader v0.0.0-20171218180944-5ea4d0ddac55/go.mod h1:GCzqZQHydohgVLSIqRKZeTt8IGb1Y4NaFfim3H40uUI=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down Expand Up @@ -185,6 +183,8 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tinode/jsonco v1.0.0 h1:zVcpjzDvjuA1G+HLrckI5EiiRyq9jgV3x37OQl6e5FE=
github.com/tinode/jsonco v1.0.0/go.mod h1:Bnavu3302Qfn2pILMNwASkelodgeew3IvDrbdzU84u8=
github.com/tinode/snowflake v1.0.0 h1:YciQ9ZKn1TrnvpS8yZErt044XJaxWVtR9aMO9rOZVOE=
github.com/tinode/snowflake v1.0.0/go.mod h1:5JiaCe3o7QdDeyRcAeZBGVghwRS+ygt2CF/hxmAoptQ=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk=
Expand Down
2 changes: 1 addition & 1 deletion server/db/mongodb/tests/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
"testing"
"time"

jcr "github.com/DisposaBoy/JsonConfigReader"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
adapter "github.com/tinode/chat/server/db"
jcr "github.com/tinode/jsonco"
b "go.mongodb.org/mongo-driver/bson"
mdb "go.mongodb.org/mongo-driver/mongo"
mdbopts "go.mongodb.org/mongo-driver/mongo/options"
Expand Down
16 changes: 7 additions & 9 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (
"strings"
"time"

// For stripping comments from JSON config
jcr "github.com/DisposaBoy/JsonConfigReader"

gh "github.com/gorilla/handlers"

// For stripping comments from JSON config
jcr "github.com/tinode/jsonco"

// Authenticators
"github.com/tinode/chat/server/auth"
_ "github.com/tinode/chat/server/auth/anon"
Expand Down Expand Up @@ -264,17 +264,15 @@ func main() {
if file, err := os.Open(*configfile); err != nil {
log.Fatal("Failed to read config file: ", err)
} else {
if err = json.NewDecoder(jcr.New(file)).Decode(&config); err != nil {
// Need to reset file to start in order to convert byte offset to line number and character position.
// Ignore possible error: can't use it anyway.
file.Seek(0, 0)
jr := jcr.New(file)
if err = json.NewDecoder(jr).Decode(&config); err != nil {
switch jerr := err.(type) {
case *json.UnmarshalTypeError:
lnum, cnum, _ := offsetToLineAndChar(file, jerr.Offset)
lnum, cnum, _ := jr.LineAndChar(jerr.Offset)
log.Fatalf("Unmarshall error in config file in %s at %d:%d (offset %d bytes): %s",
jerr.Field, lnum, cnum, jerr.Offset, jerr.Error())
case *json.SyntaxError:
lnum, cnum, _ := offsetToLineAndChar(file, jerr.Offset)
lnum, cnum, _ := jr.LineAndChar(jerr.Offset)
log.Fatalf("Syntax error in config file at %d:%d (offset %d bytes): %s",
lnum, cnum, jerr.Offset, jerr.Error())
default:
Expand Down
37 changes: 0 additions & 37 deletions server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
package main

import (
"bufio"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -746,41 +744,6 @@ func mergeMaps(dst, src map[string]interface{}) (map[string]interface{}, bool) {
return dst, changed
}

// Calculate line and character position from byte offset into a file.
func offsetToLineAndChar(r io.Reader, offset int64) (int, int, error) {
if offset < 0 {
return -1, -1, errors.New("offset value cannot be negative")
}

br := bufio.NewReader(r)

// Count lines and characters.
lnum := 1
cnum := 0
// Number of bytes consumed.
var count int64
for {
ch, size, err := br.ReadRune()
if err == io.EOF {
return -1, -1, errors.New("offset value too large")
}
count += int64(size)

if ch == '\n' {
lnum++
cnum = 0
} else {
cnum++
}

if count >= offset {
break
}
}

return lnum, cnum, nil
}

// netListener creates net.Listener for tcp and unix domains:
// if addr is is in the form "unix:/run/tinode.sock" it's a unix socket, otherwise TCP host:port.
func netListener(addr string) (net.Listener, error) {
Expand Down
20 changes: 17 additions & 3 deletions tinode-db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"strings"
"time"

jcr "github.com/DisposaBoy/JsonConfigReader"
_ "github.com/tinode/chat/server/db/mongodb"
_ "github.com/tinode/chat/server/db/mysql"
_ "github.com/tinode/chat/server/db/rethinkdb"
"github.com/tinode/chat/server/store"
jcr "github.com/tinode/jsonco"
)

type configType struct {
Expand Down Expand Up @@ -191,8 +191,22 @@ func main() {
var config configType
if file, err := os.Open(*conffile); err != nil {
log.Fatalln("Failed to read config file:", err)
} else if err = json.NewDecoder(jcr.New(file)).Decode(&config); err != nil {
log.Fatalln("Failed to parse config file:", err)
} else {
jr := jcr.New(file)
if err = json.NewDecoder(jr).Decode(&config); err != nil {
switch jerr := err.(type) {
case *json.UnmarshalTypeError:
lnum, cnum, _ := jr.LineAndChar(jerr.Offset)
log.Fatalf("Unmarshall error in config file in %s at %d:%d (offset %d bytes): %s",
jerr.Field, lnum, cnum, jerr.Offset, jerr.Error())
case *json.SyntaxError:
lnum, cnum, _ := jr.LineAndChar(jerr.Offset)
log.Fatalf("Syntax error in config file at %d:%d (offset %d bytes): %s",
lnum, cnum, jerr.Offset, jerr.Error())
default:
log.Fatal("Failed to parse config file: ", err)
}
}
}

err := store.Open(1, config.StoreConfig)
Expand Down

0 comments on commit 54dfd19

Please sign in to comment.