Skip to content

Commit

Permalink
replace toml dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
trajan0x committed Aug 24, 2022
1 parent 8985e97 commit 0db7f0f
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 5 deletions.
1 change: 1 addition & 0 deletions core/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ replace (
)

require (
github.com/BurntSushi/toml v1.1.0
github.com/brianvoe/gofakeit/v6 v6.9.0
github.com/c-bata/go-prompt v0.2.6
github.com/ipfs/go-log v1.0.5
Expand Down
5 changes: 1 addition & 4 deletions core/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6L
github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d h1:nalkkPQcITbvhmL4+C4cKA87NW0tfm3Kl9VXRoPywFg=
github.com/CosmWasm/wasmvm v0.16.6 h1:YEqno8DJifj+1NixXSJGOXCKGX4Qb4kdYK1+JIHLpog=
Expand Down Expand Up @@ -159,7 +160,6 @@ github.com/cosmos/iavl v0.17.3 h1:s2N819a2olOmiauVa0WAhoIJq9EhSXE9HDBAoR9k+8Y=
github.com/cosmos/ibc-go v1.1.5 h1:H+q6G0szM5lePgLDQbTAaKQU5UR28ZjOIA3JJjNhuPQ=
github.com/cosmos/ledger-cosmos-go v0.11.1 h1:9JIYsGnXP613pb2vPjFeMMjBI5lEDsEaF6oYorTy6J4=
github.com/cosmos/ledger-go v0.9.2 h1:Nnao/dLwaVTk1Q5U9THldpUMMXU94BOTWPddSmVB6pI=
github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand All @@ -178,7 +178,6 @@ github.com/deckarep/golang-set v1.7.1/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14y
github.com/decred/dcrd/dcrec/edwards/v2 v2.0.0 h1:E5KszxGgpjpmW8vN811G6rBAZg0/S/DftdGqN4FW5x4=
github.com/deepmap/oapi-codegen v1.6.0/go.mod h1:ryDa9AgbELGeB+YEXE1dR53yAjHwFvE9iAUlWl9Al3M=
github.com/deepmap/oapi-codegen v1.8.2/go.mod h1:YLgSKSDv/bZQB7N4ws6luhozi3cEdRktEqrX88CvjIw=
github.com/dgraph-io/badger v1.6.1 h1:w9pSFNSdq/JPM1N12Fz/F/bzo993Is1W+Q7HjPzi7yg=
github.com/dgraph-io/badger/v2 v2.2007.2 h1:EjjK0KqwaFMlPin1ajhP943VPENHJdEz1KLIegjaI3k=
github.com/dgraph-io/ristretto v0.0.3 h1:jh22xisGBjrEVnRZ1DVTpBVQm0Xndu8sMl0CWDzSIBI=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
Expand Down Expand Up @@ -659,7 +658,6 @@ github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U=
github.com/rung/go-safecast v1.0.1 h1:7rkt2qO4JGdOkWKdPEBFLaEwQy20y0IhhWJNFxmH0p0=
github.com/rung/go-safecast v1.0.1/go.mod h1:dzUcUS2UMtbfVc7w6mx/Ur3UYcpXEZC+WilISksJ4P8=
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down Expand Up @@ -732,7 +730,6 @@ github.com/tklauser/go-sysconf v0.3.5/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITn
github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA=
github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM=
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs=
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/urfave/cli/v2 v2.11.2 h1:FVfNg4m3vbjbBpLYxW//WjxUoHvJ9TlppXcqY9Q9ZfA=
github.com/urfave/cli/v2 v2.11.2/go.mod h1:f8iq5LtQ/bLxafbdBSLPPNsgaW0l/2fYYEHhAyPlwvo=
Expand Down
2 changes: 2 additions & 0 deletions core/toml/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package toml contains common utilities for implementing toml types.
package toml
89 changes: 89 additions & 0 deletions core/toml/marshaller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package toml

import (
"bytes"
"fmt"
"github.com/BurntSushi/toml"
"reflect"
"strings"
)

// Encodable is an interface for a toml with an encode method.
type Encodable interface {
Encode() (string, error)
}

// Indent is the indent we use.
const Indent = " "

// Encode is a helper method to allow you to encode a toml to text.
// config should be passed here by pointer
// TODO: use a toml parser that can poparse comments.
func Encode(config interface{}) (string, error) {
var buf bytes.Buffer
encoder := toml.NewEncoder(&buf)
encoder.Indent = Indent
err := encoder.Encode(config)
if err != nil {
return "", fmt.Errorf("could not encode file: %w", err)
}

// currently, there's a bug in the parser that requires maps to be on the same level as the parent.
// TODO: fix
splitFile := strings.Split(buf.String(), "\n")
var newLines []string
for _, line := range splitFile {
// get rid of double spacing on maps
indentLen := len(Indent) * 2
newLines = append(newLines, strings.ReplaceAll(line, getStringOfLength(indentLen), getStringOfLength(indentLen-2)))
}

return strings.Join(newLines, "\n"), nil
}

// getStringOfLength generates a blank string of length.
func getStringOfLength(length int) (res string) {
for i := 0; i < length; i++ {
res += " "
}
return res
}

// MarshalTextPtr should be a pointer here
// Deprecated: this can be done through the library.
func MarshalTextPtr(config interface{}) (text []byte, err error) {
var fieldValDeref reflect.Value

// make sure we have a pointer, if we don't return an error
rv := eindirect(reflect.ValueOf(config))
if rv.Kind() == reflect.Ptr {
// if the pointer is nil, return nothing
if rv.IsNil() {
return text, nil
}
// otherwise dereference it for decoding
rv.Set(reflect.New(rv.Type().Elem()))
fieldValDeref = rv.Elem()
} else {
return text, fmt.Errorf("this method can only be run on pointers (have %s). If your method is not a pointer, you don't need a custom text marshaller", rv.Kind().String())
}

var buf bytes.Buffer
encoder := toml.NewEncoder(&buf)
err = encoder.Encode(fieldValDeref.Interface())
if err != nil {
return text, fmt.Errorf("could not encode file: %w", err)
}
return buf.Bytes(), nil
}

// eindirect wraps a pointer.
func eindirect(v reflect.Value) reflect.Value {
// nolint: exhaustive
switch v.Kind() {
case reflect.Ptr, reflect.Interface:
return eindirect(v.Elem())
default:
return v
}
}
97 changes: 97 additions & 0 deletions core/toml/marshaller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package toml_test

import (
"encoding"
"fmt"
"github.com/brianvoe/gofakeit/v6"
. "github.com/stretchr/testify/assert"
"github.com/synapsecns/sanguine/core/toml"
)

// WorkingConfig is a config that will work.
type WorkingConfig struct {
SubConfig *WorkingSubConfig `toml:"SubConfig"`
}

// Encode enodes the sub config.
func (w *WorkingConfig) Encode() (string, error) {
//nolint: wrapcheck
return toml.Encode(w)
}

var _ toml.Encodable = &WorkingConfig{}

// WorkingSubConfig is a config with that uses the marshaller method and will work
// this will work because we wrap it in the working subconfig burntToml.
type WorkingSubConfig struct {
// OperatorKeyFile is a path to the operator key this node will use
IsaConfig bool `toml:"IsAConfig"`
// Name is the password used to decrypt the keyfile
Name string `toml:"OperatorKeyPassword"`
}

// MarshallText uses the marshall text method from marshaller
// Important: w must be a pointer.
func (w *WorkingSubConfig) MarshalText() (text []byte, err error) {
//nolint: wrapcheck
return toml.MarshalTextPtr(w)
}

var _ encoding.TextMarshaler = &WorkingSubConfig{}

// BrokenConfig is a config that will not work b/c of a missing MarshallTextPtr encode on a pointer config.
type BrokenConfig struct {
SubConfig *BrokenSubConfig `toml:"SubConfig"`
}

// Encode encodes the broken config.
func (b *BrokenConfig) Encode() (string, error) {
//nolint: wrapcheck
return toml.Encode(b)
}

var _ toml.Encodable = &BrokenConfig{}

// BrokenConfig is a broken config since it is passed by pointer but does not implement marshall text
// this will return nothing.
type BrokenSubConfig struct {
// OperatorKeyFile is a path to the operator key this node will use
IsaConfig bool `burntToml:"IsAConfig"`
// Name is the password used to decrypt the keyfile
Name string `burntToml:"OperatorKeyPassword"`
}

// generateWorkingConfig is a helper method to generate a working config with random data.
func generateWorkingConfig() *WorkingConfig {
return &WorkingConfig{
SubConfig: &WorkingSubConfig{
IsaConfig: gofakeit.Bool(),
Name: gofakeit.Name(),
}}
}

// generateBrokenCofnig generates a broken config is a helper method to generate a broken config with random data.
func generateBrokenConfig() *BrokenConfig {
return &BrokenConfig{SubConfig: &BrokenSubConfig{
IsaConfig: gofakeit.Bool(),
Name: gofakeit.Name(),
}}
}

// ExampleTestMarshallerImplementation shows how to use the marshaller function
// this is useful for when you have a config and a sub config that uses a pointer.
func ExampleMarshalTextPtr() {
// if we use a struct that doesn't use marshaller text, we see no output
brokenConfig := generateBrokenConfig()
fmt.Println(brokenConfig.Encode())

// otherwise, we see output
workingConfig := generateWorkingConfig()
fmt.Println(workingConfig.Encode())
}

func (t TomlSuite) TestExample() {
NotPanics(t.T(), func() {
ExampleMarshalTextPtr()
})
}
25 changes: 25 additions & 0 deletions core/toml/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package toml_test

import (
"github.com/stretchr/testify/suite"
"github.com/synapsecns/sanguine/core/testsuite"
"testing"
)

// TomlSuite defines the basic test suite.
type TomlSuite struct {
*testsuite.TestSuite
}

// NewTestSuite creates a new test suite and performs some basic checks afterward.
// Every test suite in the synapse library should inherit from this suite and override where necessary.
func NewTestSuite(tb testing.TB) *TomlSuite {
tb.Helper()
return &TomlSuite{
testsuite.NewTestSuite(tb),
}
}

func TestTomlSuite(t *testing.T) {
suite.Run(t, NewTestSuite(t))
}
2 changes: 1 addition & 1 deletion scribe/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/BurntSushi/toml"
tomlCommon "github.com/synapsecns/synapse-node/pkg/common/toml"
tomlCommon "github.com/synapsecns/sanguine/core/toml"
)

// Config is used to configure a Scribe instance and information about chains and contracts.
Expand Down

0 comments on commit 0db7f0f

Please sign in to comment.