-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclean.go
62 lines (51 loc) · 1.56 KB
/
clean.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package astra
// Clean cleans up the structs.
// At the moment it only changes the package name of the main package to "main".
// It also handles the "special" types.
// It also caches the service after cleaning.
func (s *Service) Clean() error {
s.Log.Info().Msg("Cleaning up structs")
mainPkg, err := s.GetMainPackageName()
if err != nil {
return err
}
for i := 0; i < len(s.Components); i++ {
s.Components[i] = s.cleanField(s.Components[i], mainPkg)
}
for i := 0; i < len(s.Routes); i++ {
for j := 0; j < len(s.Routes[i].ReturnTypes); j++ {
s.Routes[i].ReturnTypes[j].Field = s.cleanField(s.Routes[i].ReturnTypes[j].Field, mainPkg)
}
for j := 0; j < len(s.Routes[i].PathParams); j++ {
s.Routes[i].PathParams[j].Field = s.cleanField(s.Routes[i].PathParams[j].Field, mainPkg)
}
for j := 0; j < len(s.Routes[i].QueryParams); j++ {
s.Routes[i].QueryParams[j].Field = s.cleanField(s.Routes[i].QueryParams[j].Field, mainPkg)
}
for j := 0; j < len(s.Routes[i].Body); j++ {
s.Routes[i].Body[j].Field = s.cleanField(s.Routes[i].Body[j].Field, mainPkg)
}
}
s.Log.Info().Msg("Cleaning up structs complete")
if s.CacheEnabled {
err := s.Cache()
if err != nil {
s.Log.Error().Err(err).Msg("Error caching")
return err
}
}
return nil
}
func (s *Service) cleanField(f Field, mainPkg string) Field {
s.HandleSubstituteTypes(&f)
if f.Package == mainPkg {
f.Package = "main"
}
if f.MapKeyPackage == mainPkg {
f.MapKeyPackage = "main"
}
for k, v := range f.StructFields {
f.StructFields[k] = s.cleanField(v, mainPkg)
}
return f
}