Skip to content

Commit

Permalink
Fix: config package
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Nov 28, 2022
1 parent 90a7fc4 commit e0780da
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
36 changes: 36 additions & 0 deletions config/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package config

// Alias - type for aliasing in config
type Alias[Type any] struct {
name string
entity Type
}

// UnmarshalYAML -
func (a *Alias[Type]) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := unmarshal(&a.name); err == nil {
return nil
}

var typ Type
if err := unmarshal(&typ); err != nil {
return err
}
a.entity = typ
return nil
}

// Name - returns alias name if it exists
func (a *Alias[Type]) Name() string {
return a.name
}

// Struct - returns substitute struct
func (a *Alias[Type]) Struct() Type {
return a.entity
}

// SetStruct - set entity. Use in Substitute
func (a *Alias[Type]) SetStruct(entity Type) {
a.entity = entity
}
17 changes: 7 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ type Config struct {

// Substitute -
func (c *Config) Substitute() error {
if c.Hasura != nil {
c.Hasura.SetSourceName()
}
return nil
}

Expand All @@ -32,6 +29,7 @@ type DataSource struct {
Kind string `yaml:"kind"`
URL string `yaml:"url" validate:"required,url"`
Credentials *Credentials `yaml:"credentials,omitempty" validate:"omitempty"`
Timeout uint `yaml:"timeout" validate:"omitempty"`
}

// Contracts -
Expand Down Expand Up @@ -63,13 +61,12 @@ type Hasura struct {
Rest *bool `yaml:"rest"`
}

func (s *Hasura) SetSourceName() {
if s == nil {
return
}
if s.Source == "" {
s.Source = "default"
}
// UnmarshalYAML -
func (h *Hasura) UnmarshalYAML(unmarshal func(interface{}) error) error {
h.Source = "default"

type plain Hasura
return unmarshal((*plain)(h))
}

// Prometheus -
Expand Down

0 comments on commit e0780da

Please sign in to comment.