Skip to content

Commit

Permalink
Make coreDefaultsTOML private again
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-cordenier committed Jul 10, 2023
1 parent 71edd75 commit 830e674
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 27 deletions.
32 changes: 32 additions & 0 deletions core/config/docs/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package docs

import (
"log"
"strings"
"sync"

"github.com/smartcontractkit/chainlink/v2/core/config/toml"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink/cfgtest"
"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
"github.com/smartcontractkit/chainlink/v2/core/utils/config"
)

var (
defaults toml.Core
defaultsMu sync.Mutex
)

func CoreDefaults() (c toml.Core) {
defaultsMu.Lock()
defer defaultsMu.Unlock()

if (defaults == toml.Core{}) {
if err := cfgtest.DocDefaultsOnly(strings.NewReader(coreDefaultsTOML), &defaults, config.DecodeTOML); err != nil {
log.Fatalf("Failed to initialize defaults from docs: %v", err)
}
}

c.SetFrom(&defaults)
c.Database.Dialect = dialects.Postgres // not user visible - overridden for tests only
return
}
11 changes: 11 additions & 0 deletions core/config/docs/defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package docs

import (
"testing"

"github.com/smartcontractkit/chainlink/v2/core/services/chainlink/cfgtest"
)

func TestCoreDefaults_notNil(t *testing.T) {
cfgtest.AssertFieldsNotNil(t, CoreDefaults())
}
4 changes: 2 additions & 2 deletions core/config/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
//go:embed secrets.toml
secretsTOML string
//go:embed core.toml
CoreDefaultsTOML string
coreDefaultsTOML string
//go:embed chains-evm.toml
chainsEVMTOML string
//go:embed chains-cosmos.toml
Expand All @@ -38,7 +38,7 @@ var (
//go:embed example-secrets.toml
exampleSecrets string

docsTOML = CoreDefaultsTOML + chainsEVMTOML + chainsCosmosTOML + chainsSolanaTOML + chainsStarknetTOML
docsTOML = coreDefaultsTOML + chainsEVMTOML + chainsCosmosTOML + chainsSolanaTOML + chainsStarknetTOML
)

// GenerateConfig returns MarkDown documentation generated from core.toml & chains-*.toml.
Expand Down
19 changes: 0 additions & 19 deletions core/config/toml/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
_ "embed"
"errors"
"fmt"
"log"
"net"
"net/url"
"strings"
Expand All @@ -18,9 +17,7 @@ import (

"github.com/smartcontractkit/chainlink/v2/core/build"
"github.com/smartcontractkit/chainlink/v2/core/config"
"github.com/smartcontractkit/chainlink/v2/core/config/docs"
"github.com/smartcontractkit/chainlink/v2/core/config/parse"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink/cfgtest"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/p2pkey"
"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
Expand Down Expand Up @@ -58,22 +55,6 @@ type Core struct {
Insecure Insecure `toml:",omitempty"`
}

var (
defaults Core
)

func init() {
if err := cfgtest.DocDefaultsOnly(strings.NewReader(docs.CoreDefaultsTOML), &defaults, configutils.DecodeTOML); err != nil {
log.Fatalf("Failed to initialize defaults from docs: %v", err)
}
}

func CoreDefaults() (c Core) {
c.SetFrom(&defaults)
c.Database.Dialect = dialects.Postgres // not user visible - overridden for tests only
return
}

// SetFrom updates c with any non-nil values from f. (currently TOML field only!)
func (c *Core) SetFrom(f *Core) {
if v := f.ExplorerURL; v != nil {
Expand Down
5 changes: 0 additions & 5 deletions core/config/toml/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@ import (
"github.com/stretchr/testify/assert"

"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink/cfgtest"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

func TestCoreDefaults_notNil(t *testing.T) {
cfgtest.AssertFieldsNotNil(t, &defaults)
}

func TestMercurySecrets_valid(t *testing.T) {
ms := MercurySecrets{
Credentials: map[string]MercuryCredentials{
Expand Down
3 changes: 2 additions & 1 deletion core/services/chainlink/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

evmcfg "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/v2"
"github.com/smartcontractkit/chainlink/v2/core/chains/solana"
"github.com/smartcontractkit/chainlink/v2/core/config/docs"
"github.com/smartcontractkit/chainlink/v2/core/config/env"
"github.com/smartcontractkit/chainlink/v2/core/config/toml"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
Expand Down Expand Up @@ -61,7 +62,7 @@ func (c *Config) Validate() error {

// setDefaults initializes unset fields with default values.
func (c *Config) setDefaults() {
core := toml.CoreDefaults()
core := docs.CoreDefaults()
core.SetFrom(&c.Core)
c.Core = core

Expand Down

0 comments on commit 830e674

Please sign in to comment.