Skip to content

Commit e0780da

Browse files
Fix: config package
1 parent 90a7fc4 commit e0780da

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

config/alias.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package config
2+
3+
// Alias - type for aliasing in config
4+
type Alias[Type any] struct {
5+
name string
6+
entity Type
7+
}
8+
9+
// UnmarshalYAML -
10+
func (a *Alias[Type]) UnmarshalYAML(unmarshal func(interface{}) error) error {
11+
if err := unmarshal(&a.name); err == nil {
12+
return nil
13+
}
14+
15+
var typ Type
16+
if err := unmarshal(&typ); err != nil {
17+
return err
18+
}
19+
a.entity = typ
20+
return nil
21+
}
22+
23+
// Name - returns alias name if it exists
24+
func (a *Alias[Type]) Name() string {
25+
return a.name
26+
}
27+
28+
// Struct - returns substitute struct
29+
func (a *Alias[Type]) Struct() Type {
30+
return a.entity
31+
}
32+
33+
// SetStruct - set entity. Use in Substitute
34+
func (a *Alias[Type]) SetStruct(entity Type) {
35+
a.entity = entity
36+
}

config/config.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ type Config struct {
2121

2222
// Substitute -
2323
func (c *Config) Substitute() error {
24-
if c.Hasura != nil {
25-
c.Hasura.SetSourceName()
26-
}
2724
return nil
2825
}
2926

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

3735
// Contracts -
@@ -63,13 +61,12 @@ type Hasura struct {
6361
Rest *bool `yaml:"rest"`
6462
}
6563

66-
func (s *Hasura) SetSourceName() {
67-
if s == nil {
68-
return
69-
}
70-
if s.Source == "" {
71-
s.Source = "default"
72-
}
64+
// UnmarshalYAML -
65+
func (h *Hasura) UnmarshalYAML(unmarshal func(interface{}) error) error {
66+
h.Source = "default"
67+
68+
type plain Hasura
69+
return unmarshal((*plain)(h))
7370
}
7471

7572
// Prometheus -

0 commit comments

Comments
 (0)