Passed
Push — v3.0.0 ( c00ed6...da2d19 )
by Serhii
01:12
created

timeago.applyCustomTranslations   F

Complexity

Conditions 22

Size

Total Lines 70
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
eloc 36
dl 0
loc 70
rs 0
c 0
b 0
f 0
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like timeago.applyCustomTranslations often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package timeago
2
3
import (
4
	"fmt"
5
	"path"
6
	"runtime"
7
)
8
9
type LangForms map[string]string
10
11
type LangSet struct {
12
	Lang    string    `json:"lang"`
13
	Format  string    `json:"format"`
14
	Ago     string    `json:"ago"`
15
	Online  string    `json:"online"`
16
	JustNow string    `json:"justnow"`
17
	Second  LangForms `json:"second"`
18
	Minute  LangForms `json:"minute"`
19
	Hour    LangForms `json:"hour"`
20
	Day     LangForms `json:"day"`
21
	Week    LangForms `json:"week"`
22
	Month   LangForms `json:"month"`
23
	Year    LangForms `json:"year"`
24
}
25
26
func newLangSet() (*LangSet, error) {
27
	_, filename, _, ok := runtime.Caller(0)
28
29
	if !ok {
30
		return nil, createError("No called information")
31
	}
32
33
	rootPath := path.Dir(filename)
34
	filePath := fmt.Sprintf(rootPath+"/langs/%s.json", conf.Language)
35
36
	if cache, ok := cachedJsonRes[filePath]; ok {
37
		return cache, nil
38
	}
39
40
	langSet := parseLangSet(filePath)
41
	langSet = applyCustomTranslations(langSet)
42
43
	cachedJsonRes[filePath] = langSet
44
45
	return langSet, nil
46
}
47
48
func applyCustomTranslations(langSet *LangSet) *LangSet {
49
	if len(conf.Translations) == 0 {
50
		return langSet
51
	}
52
53
	for _, trans := range conf.Translations {
54
		if trans.Lang != langSet.Lang {
55
			continue
56
		}
57
58
		if trans.Format != "" {
59
			langSet.Format = trans.Format
60
		}
61
62
		if trans.Ago != "" {
63
			langSet.Ago = trans.Ago
64
		}
65
66
		if trans.Online != "" {
67
			langSet.Online = trans.Online
68
		}
69
70
		if trans.JustNow != "" {
71
			langSet.JustNow = trans.JustNow
72
		}
73
74
		if trans.Second != nil {
75
			for k, v := range trans.Second {
76
				langSet.Second[k] = v
77
			}
78
		}
79
80
		if trans.Minute != nil {
81
			for k, v := range trans.Minute {
82
				langSet.Minute[k] = v
83
			}
84
		}
85
86
		if trans.Hour != nil {
87
			for k, v := range trans.Hour {
88
				langSet.Hour[k] = v
89
			}
90
		}
91
92
		if trans.Day != nil {
93
			for k, v := range trans.Day {
94
				langSet.Day[k] = v
95
			}
96
		}
97
98
		if trans.Week != nil {
99
			for k, v := range trans.Week {
100
				langSet.Week[k] = v
101
			}
102
		}
103
104
		if trans.Month != nil {
105
			for k, v := range trans.Month {
106
				langSet.Month[k] = v
107
			}
108
		}
109
110
		if trans.Year != nil {
111
			for k, v := range trans.Year {
112
				langSet.Year[k] = v
113
			}
114
		}
115
	}
116
117
	return langSet
118
}
119