Skip to content

Commit b69280a

Browse files
committed
fix(constellation): Fix null pointer exceptions caused by invalid language
1 parent 540632d commit b69280a

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

constellation.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ var constellations = []struct {
2424

2525
// Constellation gets constellation name like "Aries", i18n is supported.
2626
func (c *Carbon) Constellation() string {
27-
if c.IsInvalid() || c.lang == nil {
27+
if c.IsInvalid() {
2828
return ""
2929
}
30+
31+
lang := c.lang
32+
if lang == nil {
33+
return ""
34+
}
35+
3036
index := -1
3137
_, month, day := c.Date()
3238
for i := 0; i < len(constellations); i++ {
@@ -39,10 +45,10 @@ func (c *Carbon) Constellation() string {
3945
}
4046
}
4147

42-
c.lang.rw.RLock()
43-
defer c.lang.rw.RUnlock()
48+
lang.rw.RLock()
49+
defer lang.rw.RUnlock()
4450

45-
if resources, ok := c.lang.resources["constellations"]; ok {
51+
if resources, ok := lang.resources["constellations"]; ok {
4652
slice := strings.Split(resources, "|")
4753
if len(slice) == MonthsPerYear {
4854
return slice[index]

outputer.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ func (c *Carbon) ToMonthString(timezone ...string) string {
3434
if c.IsInvalid() {
3535
return ""
3636
}
37-
if c.lang == nil {
37+
38+
lang := c.lang
39+
if lang == nil {
3840
return ""
3941
}
4042

41-
c.lang.rw.RLock()
42-
defer c.lang.rw.RUnlock()
43+
lang.rw.RLock()
44+
defer lang.rw.RUnlock()
4345

44-
if resources, ok := c.lang.resources["months"]; ok {
46+
if resources, ok := lang.resources["months"]; ok {
4547
slice := strings.Split(resources, "|")
4648
if len(slice) == MonthsPerYear {
4749
return slice[c.Month()-1]
@@ -58,14 +60,16 @@ func (c *Carbon) ToShortMonthString(timezone ...string) string {
5860
if c.IsInvalid() {
5961
return ""
6062
}
61-
if c.lang == nil {
63+
64+
lang := c.lang
65+
if lang == nil {
6266
return ""
6367
}
6468

65-
c.lang.rw.RLock()
66-
defer c.lang.rw.RUnlock()
69+
lang.rw.RLock()
70+
defer lang.rw.RUnlock()
6771

68-
if resources, ok := c.lang.resources["short_months"]; ok {
72+
if resources, ok := lang.resources["short_months"]; ok {
6973
slice := strings.Split(resources, "|")
7074
if len(slice) == MonthsPerYear {
7175
return slice[c.Month()-1]
@@ -82,14 +86,16 @@ func (c *Carbon) ToWeekString(timezone ...string) string {
8286
if c.IsInvalid() {
8387
return ""
8488
}
85-
if c.lang == nil {
89+
90+
lang := c.lang
91+
if lang == nil {
8692
return ""
8793
}
8894

89-
c.lang.rw.RLock()
90-
defer c.lang.rw.RUnlock()
95+
lang.rw.RLock()
96+
defer lang.rw.RUnlock()
9197

92-
if resources, ok := c.lang.resources["weeks"]; ok {
98+
if resources, ok := lang.resources["weeks"]; ok {
9399
slice := strings.Split(resources, "|")
94100
if len(slice) == DaysPerWeek {
95101
return slice[c.DayOfWeek()%DaysPerWeek]
@@ -106,14 +112,16 @@ func (c *Carbon) ToShortWeekString(timezone ...string) string {
106112
if c.IsInvalid() {
107113
return ""
108114
}
109-
if c.lang == nil {
115+
116+
lang := c.lang
117+
if lang == nil {
110118
return ""
111119
}
112120

113-
c.lang.rw.RLock()
114-
defer c.lang.rw.RUnlock()
121+
lang.rw.RLock()
122+
defer lang.rw.RUnlock()
115123

116-
if resources, ok := c.lang.resources["short_weeks"]; ok {
124+
if resources, ok := lang.resources["short_weeks"]; ok {
117125
slice := strings.Split(resources, "|")
118126
if len(slice) == DaysPerWeek {
119127
return slice[c.DayOfWeek()%DaysPerWeek]

season.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ func (c *Carbon) Season() string {
2525
if c.IsInvalid() {
2626
return ""
2727
}
28-
if c.lang == nil {
28+
29+
lang := c.lang
30+
if lang == nil {
2931
return ""
3032
}
3133

32-
c.lang.rw.RLock()
33-
defer c.lang.rw.RUnlock()
34+
lang.rw.RLock()
35+
defer lang.rw.RUnlock()
3436

35-
if resources, ok := c.lang.resources["seasons"]; ok {
37+
if resources, ok := lang.resources["seasons"]; ok {
3638
slice := strings.Split(resources, "|")
3739
if len(slice) == QuartersPerYear {
3840
return slice[seasons[c.Month()]]

0 commit comments

Comments
 (0)