Skip to content

Commit

Permalink
Make anything that doesn't need to be exported private (closes #111)
Browse files Browse the repository at this point in the history
  • Loading branch information
TiganeteaRobert authored and colmsnowplow committed Jul 22, 2022
1 parent 80e58ef commit 7c658ad
Show file tree
Hide file tree
Showing 26 changed files with 307 additions and 166 deletions.
12 changes: 6 additions & 6 deletions config/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ type Pluggable interface {
ComponentCreator
}

// DecodingHandler is the type of any function that, given a ComponentConfigurable
// decodingHandler is the type of any function that, given a ComponentConfigurable
// and a Decoder, returns a pointer to a structure that was decoded.
type DecodingHandler func(c ComponentConfigurable, d Decoder) (interface{}, error)
type decodingHandler func(c ComponentConfigurable, d Decoder) (interface{}, error)

// WithDecoderOptions returns a DecodingHandler closed over some DecoderOptions.
func WithDecoderOptions(opts *DecoderOptions) DecodingHandler {
// withDecoderOptions returns a decodingHandler closed over some DecoderOptions.
func withDecoderOptions(opts *DecoderOptions) decodingHandler {
return func(c ComponentConfigurable, d Decoder) (interface{}, error) {
return Configure(c, d, opts)
return configure(c, d, opts)
}
}

// Configure returns the decoded target.
func Configure(c ComponentConfigurable, d Decoder, opts *DecoderOptions) (interface{}, error) {
func configure(c ComponentConfigurable, d Decoder, opts *DecoderOptions) (interface{}, error) {
target, err := c.ProvideDefault() // target is ptr
if err != nil {
return nil, err
Expand Down
98 changes: 49 additions & 49 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,86 +27,86 @@ import (
"github.com/snowplow-devops/stream-replicator/pkg/target/targetiface"
)

// Config holds the configuration data along with the decoder to decode them
// Config holds the configuration data along with the Decoder to Decode them
type Config struct {
Data *ConfigurationData
Data *configurationData
Decoder Decoder
}

// ConfigurationData for holding all configuration options
type ConfigurationData struct {
Source *Component `hcl:"source,block" envPrefix:"SOURCE_"`
Target *Component `hcl:"target,block" envPrefix:"TARGET_"`
FailureTarget *FailureConfig `hcl:"failure_target,block"`
Sentry *SentryConfig `hcl:"sentry,block"`
StatsReceiver *StatsConfig `hcl:"stats_receiver,block"`
Transformation string `hcl:"message_transformation,optional" env:"MESSAGE_TRANSFORMATION"`
LogLevel string `hcl:"log_level,optional" env:"LOG_LEVEL"`
GoogleServiceAccountB64 string `hcl:"google_application_credentials_b64,optional" env:"GOOGLE_APPLICATION_CREDENTIALS_B64"`
UserProvidedID string `hcl:"user_provided_id,optional" env:"USER_PROVIDED_ID"`
// configurationData for holding all configuration options
type configurationData struct {
Source *component `hcl:"source,block" envPrefix:"SOURCE_"`
Target *component `hcl:"target,block" envPrefix:"TARGET_"`
FailureTarget *failureConfig `hcl:"failure_target,block"`
Sentry *sentryConfig `hcl:"sentry,block"`
StatsReceiver *statsConfig `hcl:"stats_receiver,block"`
Transform *TransformConfig `hcl:"transform,block"`
LogLevel string `hcl:"log_level,optional" env:"LOG_LEVEL"`
GoogleServiceAccountB64 string `hcl:"google_application_credentials_b64,optional" env:"GOOGLE_APPLICATION_CREDENTIALS_B64"`
UserProvidedID string `hcl:"user_provided_id,optional" env:"USER_PROVIDED_ID"`
EnableTelemetry bool `hcl:"enable_telemetry,optional" env:"ENABLE_TELEMETRY"`
}

// Component is a type to abstract over configuration blocks.
type Component struct {
Use *Use `hcl:"use,block"`
// component is a type to abstract over configuration blocks.
type component struct {
Use *use `hcl:"use,block"`
}

// Use is a type to denote what a component will be configured to use.
type Use struct {
// use is a type to denote what a component will be configured to use.
type use struct {
Name string `hcl:",label" env:"NAME"`
Body hcl.Body `hcl:",remain"`
}

// FailureConfig holds configuration for the failure target.
// failureConfig holds configuration for the failure target.
// It includes the target component to use.
type FailureConfig struct {
Target *Use `hcl:"use,block" envPrefix:"FAILURE_TARGET_"`
type failureConfig struct {
Target *use `hcl:"use,block" envPrefix:"FAILURE_TARGET_"`
Format string `hcl:"format,optional" env:"FAILURE_TARGETS_FORMAT"`
}

// SentryConfig configures the Sentry error tracker.
type SentryConfig struct {
// sentryConfig configures the Sentry error tracker.
type sentryConfig struct {
Dsn string `hcl:"dsn" env:"SENTRY_DSN"`
Tags string `hcl:"tags,optional" env:"SENTRY_TAGS"`
Debug bool `hcl:"debug,optional" env:"SENTRY_DEBUG"`
}

// StatsConfig holds configuration for stats receivers.
// statsConfig holds configuration for stats receivers.
// It includes a receiver component to use.
type StatsConfig struct {
Receiver *Use `hcl:"use,block" envPrefix:"STATS_RECEIVER_"`
type statsConfig struct {
Receiver *use `hcl:"use,block" envPrefix:"STATS_RECEIVER_"`
TimeoutSec int `hcl:"timeout_sec,optional" env:"STATS_RECEIVER_TIMEOUT_SEC"`
BufferSec int `hcl:"buffer_sec,optional" env:"STATS_RECEIVER_BUFFER_SEC"`
}

// TransformConfig holds configuration for tranformations.
type TransformConfig struct {
Message string `hcl:"message_transformation,optional" env:"MESSAGE_TRANSFORMATION"`
Layer *Use `hcl:"use,block" envPrefix:"TRANSFORMATION_LAYER_"`
Layer *use `hcl:"use,block" envPrefix:"TRANSFORMATION_LAYER_"`
}

// defaultConfigData returns the initial main configuration target.
func defaultConfigData() *ConfigurationData {
return &ConfigurationData{
Source: &Component{&Use{Name: "stdin"}},
Target: &Component{&Use{Name: "stdout"}},
func defaultConfigData() *configurationData {
return &configurationData{
Source: &component{&use{Name: "stdin"}},
Target: &component{&use{Name: "stdout"}},

FailureTarget: &FailureConfig{
Target: &Use{Name: "stdout"},
FailureTarget: &failureConfig{
Target: &use{Name: "stdout"},
Format: "snowplow",
},
Sentry: &SentryConfig{
Sentry: &sentryConfig{
Tags: "{}",
},
StatsReceiver: &StatsConfig{
Receiver: &Use{},
StatsReceiver: &statsConfig{
Receiver: &use{},
TimeoutSec: 1,
BufferSec: 15,
},
Transform: &TransformConfig{
Message: "none",
Layer: &Use{},
Layer: &use{},
},
LogLevel: "info",
EnableTelemetry: true,
Expand All @@ -132,7 +132,7 @@ func newEnvConfig() (*Config, error) {
var err error

decoderOpts := &DecoderOptions{}
envDecoder := &EnvDecoder{}
envDecoder := &envDecoder{}

configData := defaultConfigData()

Expand Down Expand Up @@ -163,12 +163,12 @@ func newHclConfig(filename string) (*Config, error) {
}

// Creating EvalContext
evalContext := CreateHclContext() // ptr
evalContext := createHclContext() // ptr

// Decoding
configData := defaultConfigData()
decoderOpts := &DecoderOptions{Input: fileHCL.Body}
hclDecoder := &HclDecoder{EvalContext: evalContext}
hclDecoder := &hclDecoder{EvalContext: evalContext}

err = hclDecoder.Decode(decoderOpts, configData)
if err != nil {
Expand All @@ -183,9 +183,9 @@ func newHclConfig(filename string) (*Config, error) {
return &mainConfig, nil
}

// CreateComponent creates a pluggable component given the decoder options.
// CreateComponent creates a pluggable component given the Decoder options.
func (c *Config) CreateComponent(p Pluggable, opts *DecoderOptions) (interface{}, error) {
componentConfigure := WithDecoderOptions(opts)
componentConfigure := withDecoderOptions(opts)

decodedConfig, err := componentConfigure(p, c.Decoder)
if err != nil {
Expand All @@ -206,7 +206,7 @@ func (c *Config) GetTarget() (targetiface.Target, error) {
switch useTarget.Name {
case "stdout":
plug = target.AdaptStdoutTargetFunc(
target.NewStdoutTarget,
target.StdoutTargetConfigFunction,
)
case "kinesis":
plug = target.AdaptKinesisTargetFunc(
Expand All @@ -226,7 +226,7 @@ func (c *Config) GetTarget() (targetiface.Target, error) {
)
case "eventhub":
plug = target.AdaptEventHubTargetFunc(
target.NewEventHubTarget,
target.EventHubTargetConfigFunction,
)
case "http":
plug = target.AdaptHTTPTargetFunc(
Expand Down Expand Up @@ -262,7 +262,7 @@ func (c *Config) GetFailureTarget(AppName string, AppVersion string) (failureifa
switch useFailureTarget.Name {
case "stdout":
plug = target.AdaptStdoutTargetFunc(
target.NewStdoutTarget,
target.StdoutTargetConfigFunction,
)
case "kinesis":
plug = target.AdaptKinesisTargetFunc(
Expand All @@ -282,7 +282,7 @@ func (c *Config) GetFailureTarget(AppName string, AppVersion string) (failureifa
)
case "eventhub":
plug = target.AdaptEventHubTargetFunc(
target.NewEventHubTarget,
target.EventHubTargetConfigFunction,
)
case "http":
plug = target.AdaptHTTPTargetFunc(
Expand Down Expand Up @@ -331,15 +331,15 @@ func (c *Config) GetTags() (map[string]string, error) {
// GetObserver builds and returns the observer with the embedded
// optional stats receiver
func (c *Config) GetObserver(tags map[string]string) (*observer.Observer, error) {
sr, err := c.GetStatsReceiver(tags)
sr, err := c.getStatsReceiver(tags)
if err != nil {
return nil, err
}
return observer.New(sr, time.Duration(c.Data.StatsReceiver.TimeoutSec)*time.Second, time.Duration(c.Data.StatsReceiver.BufferSec)*time.Second), nil
}

// GetStatsReceiver builds and returns the stats receiver
func (c *Config) GetStatsReceiver(tags map[string]string) (statsreceiveriface.StatsReceiver, error) {
// getStatsReceiver builds and returns the stats receiver
func (c *Config) getStatsReceiver(tags map[string]string) (statsreceiveriface.StatsReceiver, error) {
useReceiver := c.Data.StatsReceiver.Receiver
decoderOpts := &DecoderOptions{
Input: useReceiver.Body,
Expand Down
20 changes: 10 additions & 10 deletions config/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ type DecoderOptions struct {
Input hcl.Body
}

// EnvDecoder implements Decoder.
type EnvDecoder struct{}
// envDecoder implements Decoder.
type envDecoder struct{}

// Decode populates target from the environment.
// The target argument must be a pointer to a struct type value.
func (e *EnvDecoder) Decode(opts *DecoderOptions, target interface{}) error {
func (e *envDecoder) Decode(opts *DecoderOptions, target interface{}) error {
// Decoder Options cannot be missing
if opts == nil {
return errors.New("missing DecoderOptions for EnvDecoder")
return errors.New("missing DecoderOptions for envDecoder")
}

// If target is nil then we assume that target is not decodable.
Expand All @@ -56,19 +56,19 @@ func (e *EnvDecoder) Decode(opts *DecoderOptions, target interface{}) error {
return env.Parse(target, envOpts)
}

// HclDecoder implements Decoder.
type HclDecoder struct {
// hclDecoder implements Decoder.
type hclDecoder struct {
EvalContext *hcl.EvalContext
}

// Decode populates target given HCL input through DecoderOptions.
// The target argument must be a pointer to an allocated structure.
// If the HCL input is nil, we assume there is nothing to do and the target
// stays unaffected. If the target is nil, we assume is not decodable.
func (h *HclDecoder) Decode(opts *DecoderOptions, target interface{}) error {
func (h *hclDecoder) Decode(opts *DecoderOptions, target interface{}) error {
// Decoder Options cannot be missing
if opts == nil {
return errors.New("missing DecoderOptions for HclDecoder")
return errors.New("missing DecoderOptions for hclDecoder")
}

src := opts.Input
Expand All @@ -90,12 +90,12 @@ func (h *HclDecoder) Decode(opts *DecoderOptions, target interface{}) error {
return nil
}

// CreateHclContext creates an *hcl.EvalContext that is used in decoding HCL.
// createHclContext creates an *hcl.EvalContext that is used in decoding HCL.
// Here we can add the evaluation features available for the HCL configuration
// users.
// For now, below is an example of 2 different ways users can reference
// environment variables in their HCL configuration file.
func CreateHclContext() *hcl.EvalContext {
func createHclContext() *hcl.EvalContext {
evalCtx := &hcl.EvalContext{
Functions: hclCtxFunctions(),
Variables: hclCtxVariables(),
Expand Down
8 changes: 4 additions & 4 deletions config/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type testStruct struct {
}

func TestEnvDecode(t *testing.T) {
envDecoder := EnvDecoder{}
envDecoder := envDecoder{}

testCases := []struct {
TestName string
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestEnvDecode(t *testing.T) {

func TestHclDecode(t *testing.T) {
evalCtx := &hcl.EvalContext{}
hclDecoder := HclDecoder{evalCtx}
hclDecoder := hclDecoder{evalCtx}
hclSrc := `
test_string = "ateststring"
`
Expand Down Expand Up @@ -147,8 +147,8 @@ func TestCreateHclContext(t *testing.T) {
TestInt int `hcl:"test_int"`
}

evalCtx := CreateHclContext()
hclDecoder := HclDecoder{evalCtx}
evalCtx := createHclContext()
hclDecoder := hclDecoder{evalCtx}
hclSrc := `
test_string = env.TEST_STRING
test_int = env("TEST_INT")
Expand Down
8 changes: 4 additions & 4 deletions pkg/models/filter_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ type FilterResult struct {
AvgFilterLatency time.Duration
}

// NewFilterResult uses the current time as the timeOfFilter and calls NewFilterResultWithTime
// NewFilterResult uses the current time as the timeOfFilter and calls newFilterResultWithTime
func NewFilterResult(filtered []*Message) *FilterResult {
return NewFilterResultWithTime(filtered, time.Now().UTC())
return newFilterResultWithTime(filtered, time.Now().UTC())
}

// NewFilterResultWithTime builds a result structure to return from a filtered message slice
// newFilterResultWithTime builds a result structure to return from a filtered message slice
// attempt which contains the filtered message count as well as several
// derived latency measures.
func NewFilterResultWithTime(filtered []*Message, timeOfFilter time.Time) *FilterResult {
func newFilterResultWithTime(filtered []*Message, timeOfFilter time.Time) *FilterResult {
r := FilterResult{
FilteredCount: int64(len(filtered)),
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/models/filter_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestNewFilterResult_EmptyWithoutTime(t *testing.T) {
func TestNewFilterResult_EmptyWithTime(t *testing.T) {
assert := assert.New(t)

r := NewFilterResultWithTime(nil, time.Now().UTC())
r := newFilterResultWithTime(nil, time.Now().UTC())
assert.NotNil(r)

assert.Equal(int64(0), r.FilteredCount)
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestNewFilterResult_WithMessages(t *testing.T) {
},
}

r := NewFilterResultWithTime(filtered, timeNow)
r := newFilterResultWithTime(filtered, timeNow)
assert.NotNil(r)

assert.Equal(int64(2), r.FilteredCount)
Expand Down
Loading

0 comments on commit 7c658ad

Please sign in to comment.