Skip to content

Commit

Permalink
Make accountDefaultsJSON a private config member
Browse files Browse the repository at this point in the history
  • Loading branch information
laurb9 committed Aug 31, 2020
1 parent edaa2aa commit dc8a74d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
23 changes: 18 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ type Configuration struct {
// AccountDefaults defines default settings for valid accounts that are partially defined
// and provides a way to set global settings that can be overridden at account level.
AccountDefaults Account `mapstructure:"account_defaults"`
// AccountDefaultsJSON is the serialized form of AccountDefaults used for json merge
AccountDefaultsJSON json.RawMessage
// accountDefaultsJSON is the internal serialized form of AccountDefaults used for json merge
accountDefaultsJSON json.RawMessage
// Local private file containing SSL certificates
PemCertsFile string `mapstructure:"certificates_file"`
// Custom headers to handle request timeouts from queueing infrastructure
Expand Down Expand Up @@ -597,9 +597,8 @@ func New(v *viper.Viper) (*Configuration, error) {

// Update account defaults and generate base json for patch
c.AccountDefaults.CacheTTL = c.CacheURL.DefaultTTLs // comment this out to set explicitly in config
var err error
if c.AccountDefaultsJSON, err = json.Marshal(c.AccountDefaults); err != nil {
glog.Warningf("converting account_defaults to json: %v", err)
if err := c.MarshalAccountDefaults(); err != nil {
return nil, err
}

// To look for a request's publisher_id in the NonStandardPublishers list in
Expand Down Expand Up @@ -632,6 +631,20 @@ func New(v *viper.Viper) (*Configuration, error) {
return &c, nil
}

// MarshalAccountDefaults compiles AccountDefaults into the JSON format used for merge patch
func (cfg *Configuration) MarshalAccountDefaults() error {
var err error
if cfg.accountDefaultsJSON, err = json.Marshal(cfg.AccountDefaults); err != nil {
glog.Warningf("converting %+v to json: %v", cfg.AccountDefaults, err)
}
return err
}

// GetAccountDefaultsJSON returns the precompiled JSON form of account_defaults
func (cfg *Configuration) GetAccountDefaultsJSON() json.RawMessage {
return cfg.accountDefaultsJSON
}

//Allows for protocol relative URL if scheme is empty
func (cfg *Cache) GetBaseURL() string {
cfg.Scheme = strings.ToLower(cfg.Scheme)
Expand Down
2 changes: 1 addition & 1 deletion endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ func (deps *endpointDeps) getAccount(ctx context.Context, pubID string) (account
} else {
// pubID resolved to a valid account, merge with AccountDefaults for a complete config
account = &config.Account{}
completeJSON, err := jsonpatch.MergePatch(deps.cfg.AccountDefaultsJSON, accountJSON)
completeJSON, err := jsonpatch.MergePatch(deps.cfg.GetAccountDefaultsJSON(), accountJSON)
if err == nil {
err = json.Unmarshal(completeJSON, account)
}
Expand Down
2 changes: 1 addition & 1 deletion endpoints/openrtb2/auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (gr *getResponseFromDirectory) doRequest(t *testing.T, requestData []byte)
AccountRequired: gr.accountReq,
AccountDefaults: config.Account{Disabled: gr.accountDefaultDisabled},
}
cfg.AccountDefaultsJSON, _ = json.Marshal(cfg.AccountDefaults)
assert.NoError(t, cfg.MarshalAccountDefaults())
endpoint, _ := NewEndpoint(
&nobidExchange{},
newParamsValidator(t),
Expand Down

0 comments on commit dc8a74d

Please sign in to comment.