diff --git a/config/alias.go b/config/alias.go new file mode 100644 index 0000000..18d772a --- /dev/null +++ b/config/alias.go @@ -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 +} diff --git a/config/config.go b/config/config.go index 9494b53..db3d944 100644 --- a/config/config.go +++ b/config/config.go @@ -21,9 +21,6 @@ type Config struct { // Substitute - func (c *Config) Substitute() error { - if c.Hasura != nil { - c.Hasura.SetSourceName() - } return nil } @@ -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 - @@ -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 -