Skip to content
This repository was archived by the owner on Feb 17, 2023. It is now read-only.

Commit 7286ca6

Browse files
committed
Remove fields from OSInfo, restructure code
1 parent 3335cae commit 7286ca6

File tree

2 files changed

+50
-52
lines changed

2 files changed

+50
-52
lines changed

all_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ var uastrings = []struct {
9090
title: "IE10",
9191
ua: "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)",
9292
expected: "Mozilla:5.0 Platform:Windows OS:Windows 8 Browser:Internet Explorer-10.0 Engine:Trident Bot:false Mobile:false",
93-
expectedOS: &OSInfo{"Windows 8", "Windows", "8", "", "Windows"},
93+
expectedOS: &OSInfo{"Windows 8", "Windows", "8"},
9494
},
9595
{
9696
title: "Tablet",
@@ -106,19 +106,19 @@ var uastrings = []struct {
106106
title: "Phone",
107107
ua: "Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; SAMSUNG; SGH-i917)",
108108
expected: "Mozilla:4.0 Platform:Windows OS:Windows Phone OS 7.0 Browser:Internet Explorer-7.0 Engine:Trident Bot:false Mobile:true",
109-
expectedOS: &OSInfo{"Windows Phone OS 7.0", "Windows Phone OS", "7.0", "", "Windows"},
109+
expectedOS: &OSInfo{"Windows Phone OS 7.0", "Windows Phone OS", "7.0"},
110110
},
111111
{
112112
title: "IE6",
113113
ua: "Mozilla/4.0 (compatible; MSIE6.0; Windows NT 5.0; .NET CLR 1.1.4322)",
114114
expected: "Mozilla:4.0 Platform:Windows OS:Windows 2000 Browser:Internet Explorer-6.0 Engine:Trident Bot:false Mobile:false",
115-
expectedOS: &OSInfo{"Windows 2000", "Windows", "2000", "", "Windows"},
115+
expectedOS: &OSInfo{"Windows 2000", "Windows", "2000"},
116116
},
117117
{
118118
title: "IE8Compatibility",
119119
ua: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3; MS-RTC LM 8)",
120120
expected: "Mozilla:4.0 Platform:Windows OS:Windows 7 Browser:Internet Explorer-8.0 Engine:Trident Bot:false Mobile:false",
121-
expectedOS: &OSInfo{"Windows 7", "Windows", "7", "", "Windows"},
121+
expectedOS: &OSInfo{"Windows 7", "Windows", "7"},
122122
},
123123
{
124124
title: "IE10Compatibility",
@@ -129,7 +129,7 @@ var uastrings = []struct {
129129
title: "IE11Win81",
130130
ua: "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko",
131131
expected: "Mozilla:5.0 Platform:Windows OS:Windows 8.1 Browser:Internet Explorer-11.0 Engine:Trident Bot:false Mobile:false",
132-
expectedOS: &OSInfo{"Windows 8.1", "Windows", "8.1", "", "Windows"},
132+
expectedOS: &OSInfo{"Windows 8.1", "Windows", "8.1"},
133133
},
134134
{
135135
title: "IE11Win7",
@@ -157,7 +157,7 @@ var uastrings = []struct {
157157
title: "EdgeDesktop",
158158
ua: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240",
159159
expected: "Mozilla:5.0 Platform:Windows OS:Windows 10 Browser:Edge-12.10240 Engine:EdgeHTML Bot:false Mobile:false",
160-
expectedOS: &OSInfo{"Windows 10", "Windows", "10", "", "Windows"},
160+
expectedOS: &OSInfo{"Windows 10", "Windows", "10"},
161161
},
162162
{
163163
title: "EdgeMobile",
@@ -170,31 +170,31 @@ var uastrings = []struct {
170170
title: "FirefoxMac",
171171
ua: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0b8) Gecko/20100101 Firefox/4.0b8",
172172
expected: "Mozilla:5.0 Platform:Macintosh OS:Intel Mac OS X 10.6 Browser:Firefox-4.0b8 Engine:Gecko-20100101 Bot:false Mobile:false",
173-
expectedOS: &OSInfo{"Intel Mac OS X 10.6", "Mac OS X", "10.6", "", "Macintosh"},
173+
expectedOS: &OSInfo{"Intel Mac OS X 10.6", "Mac OS X", "10.6"},
174174
},
175175
{
176176
title: "FirefoxMacLoc",
177177
ua: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13",
178178
expected: "Mozilla:5.0 Platform:Macintosh OS:Intel Mac OS X 10.6 Localization:en-US Browser:Firefox-3.6.13 Engine:Gecko-20101203 Bot:false Mobile:false",
179-
expectedOS: &OSInfo{"Intel Mac OS X 10.6", "Mac OS X", "10.6", "en-US", "Macintosh"},
179+
expectedOS: &OSInfo{"Intel Mac OS X 10.6", "Mac OS X", "10.6"},
180180
},
181181
{
182182
title: "FirefoxLinux",
183183
ua: "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0",
184184
expected: "Mozilla:5.0 Platform:X11 OS:Linux x86_64 Browser:Firefox-17.0 Engine:Gecko-20100101 Bot:false Mobile:false",
185-
expectedOS: &OSInfo{"Linux x86_64", "Linux", "", "", "X11"},
185+
expectedOS: &OSInfo{"Linux x86_64", "Linux", ""},
186186
},
187187
{
188188
title: "FirefoxLinux - Ubuntu V50",
189189
ua: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0",
190190
expected: "Mozilla:5.0 Platform:X11 OS:Ubuntu Browser:Firefox-50.0 Engine:Gecko-20100101 Bot:false Mobile:false",
191-
expectedOS: &OSInfo{"Ubuntu", "Ubuntu", "", "", "X11"},
191+
expectedOS: &OSInfo{"Ubuntu", "Ubuntu", ""},
192192
},
193193
{
194194
title: "FirefoxWin",
195195
ua: "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14",
196196
expected: "Mozilla:5.0 Platform:Windows OS:Windows XP Localization:en-US Browser:Firefox-2.0.0.14 Engine:Gecko-20080404 Bot:false Mobile:false",
197-
expectedOS: &OSInfo{"Windows XP", "Windows", "XP", "en-US", "Windows"},
197+
expectedOS: &OSInfo{"Windows XP", "Windows", "XP"},
198198
},
199199
{
200200
title: "Firefox29Win7",
@@ -205,13 +205,13 @@ var uastrings = []struct {
205205
title: "CaminoMac",
206206
ua: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en; rv:1.8.1.14) Gecko/20080409 Camino/1.6 (like Firefox/2.0.0.14)",
207207
expected: "Mozilla:5.0 Platform:Macintosh OS:Intel Mac OS X Localization:en Browser:Camino-1.6 Engine:Gecko-20080409 Bot:false Mobile:false",
208-
expectedOS: &OSInfo{"Intel Mac OS X", "Mac OS X", "", "en", "Macintosh"},
208+
expectedOS: &OSInfo{"Intel Mac OS X", "Mac OS X", ""},
209209
},
210210
{
211211
title: "Iceweasel",
212212
ua: "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1) Gecko/20061024 Iceweasel/2.0 (Debian-2.0+dfsg-1)",
213213
expected: "Mozilla:5.0 Platform:X11 OS:Linux i686 Localization:en-US Browser:Iceweasel-2.0 Engine:Gecko-20061024 Bot:false Mobile:false",
214-
expectedOS: &OSInfo{"Linux i686", "Linux", "", "en-US", "X11"},
214+
expectedOS: &OSInfo{"Linux i686", "Linux", ""},
215215
},
216216
{
217217
title: "SeaMonkey",
@@ -227,13 +227,13 @@ var uastrings = []struct {
227227
title: "AndroidFirefoxTablet",
228228
ua: "Mozilla/5.0 (Android; Tablet; rv:26.0) Gecko/26.0 Firefox/26.0",
229229
expected: "Mozilla:5.0 Platform:Tablet OS:Android Browser:Firefox-26.0 Engine:Gecko-26.0 Bot:false Mobile:true",
230-
expectedOS: &OSInfo{"Android", "Android", "", "", "Tablet"},
230+
expectedOS: &OSInfo{"Android", "Android", ""},
231231
},
232232
{
233233
title: "FirefoxOS",
234234
ua: "Mozilla/5.0 (Mobile; rv:26.0) Gecko/26.0 Firefox/26.0",
235235
expected: "Mozilla:5.0 Platform:Mobile OS:FirefoxOS Browser:Firefox-26.0 Engine:Gecko-26.0 Bot:false Mobile:true",
236-
expectedOS: &OSInfo{"FirefoxOS", "FirefoxOS", "", "", "Mobile"},
236+
expectedOS: &OSInfo{"FirefoxOS", "FirefoxOS", ""},
237237
},
238238
{
239239
title: "FirefoxOSTablet",
@@ -244,7 +244,7 @@ var uastrings = []struct {
244244
title: "FirefoxWinXP",
245245
ua: "Mozilla/5.0 (Windows NT 5.2; rv:31.0) Gecko/20100101 Firefox/31.0",
246246
expected: "Mozilla:5.0 Platform:Windows OS:Windows XP x64 Edition Browser:Firefox-31.0 Engine:Gecko-20100101 Bot:false Mobile:false",
247-
expectedOS: &OSInfo{"Windows XP x64 Edition", "Windows", "XP", "", "Windows"},
247+
expectedOS: &OSInfo{"Windows XP x64 Edition", "Windows", "XP"},
248248
},
249249
{
250250
title: "FirefoxMRA",
@@ -257,7 +257,7 @@ var uastrings = []struct {
257257
title: "OperaMac",
258258
ua: "Opera/9.27 (Macintosh; Intel Mac OS X; U; en)",
259259
expected: "Platform:Macintosh OS:Intel Mac OS X Localization:en Browser:Opera-9.27 Engine:Presto Bot:false Mobile:false",
260-
expectedOS: &OSInfo{"Intel Mac OS X", "Mac OS X", "", "en", "Macintosh"},
260+
expectedOS: &OSInfo{"Intel Mac OS X", "Mac OS X", ""},
261261
},
262262
{
263263
title: "OperaWin",
@@ -273,7 +273,7 @@ var uastrings = []struct {
273273
title: "OperaWin2Comment",
274274
ua: "Opera/9.80 (Windows NT 6.0; WOW64) Presto/2.12.388 Version/12.15",
275275
expected: "Platform:Windows OS:Windows Vista Browser:Opera-9.80 Engine:Presto-2.12.388 Bot:false Mobile:false",
276-
expectedOS: &OSInfo{"Windows Vista", "Windows", "Vista", "", "Windows"},
276+
expectedOS: &OSInfo{"Windows Vista", "Windows", "Vista"},
277277
},
278278
{
279279
title: "OperaMinimal",
@@ -294,13 +294,13 @@ var uastrings = []struct {
294294
title: "OperaLinux - Ubuntu V41",
295295
ua: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36 OPR/41.0.2353.69",
296296
expected: "Mozilla:5.0 Platform:X11 OS:Linux x86_64 Browser:Opera-41.0.2353.69 Engine:AppleWebKit-537.36 Bot:false Mobile:false",
297-
expectedOS: &OSInfo{"Linux x86_64", "Linux", "", "", "X11"},
297+
expectedOS: &OSInfo{"Linux x86_64", "Linux", ""},
298298
},
299299
{
300300
title: "OperaAndroid",
301301
ua: "Opera/9.80 (Android 4.2.1; Linux; Opera Mobi/ADR-1212030829) Presto/2.11.355 Version/12.10",
302302
expected: "Platform:Android 4.2.1 OS:Linux Browser:Opera-9.80 Engine:Presto-2.11.355 Bot:false Mobile:true",
303-
expectedOS: &OSInfo{"Linux", "Linux", "", "", "Android 4.2.1"},
303+
expectedOS: &OSInfo{"Linux", "Linux", ""},
304304
},
305305
{
306306
title: "OperaNested",
@@ -360,7 +360,7 @@ var uastrings = []struct {
360360
title: "ChromeLinux",
361361
ua: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11",
362362
expected: "Mozilla:5.0 Platform:X11 OS:Linux x86_64 Browser:Chrome-23.0.1271.97 Engine:AppleWebKit-537.11 Bot:false Mobile:false",
363-
expectedOS: &OSInfo{"Linux x86_64", "Linux", "", "", "X11"},
363+
expectedOS: &OSInfo{"Linux x86_64", "Linux", ""},
364364
},
365365
{
366366
title: "ChromeLinux - Ubuntu V55",
@@ -381,7 +381,7 @@ var uastrings = []struct {
381381
title: "ChromeMac",
382382
ua: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.231 Safari/534.10",
383383
expected: "Mozilla:5.0 Platform:Macintosh OS:Intel Mac OS X 10_6_5 Localization:en-US Browser:Chrome-8.0.552.231 Engine:AppleWebKit-534.10 Bot:false Mobile:false",
384-
expectedOS: &OSInfo{"Intel Mac OS X 10_6_5", "Mac OS X", "10.6.5", "en-US", "Macintosh"},
384+
expectedOS: &OSInfo{"Intel Mac OS X 10_6_5", "Mac OS X", "10.6.5"},
385385
},
386386
{
387387
title: "SafariMac",
@@ -397,7 +397,7 @@ var uastrings = []struct {
397397
title: "iPhone7",
398398
ua: "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_3 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B511 Safari/9537.53",
399399
expected: "Mozilla:5.0 Platform:iPhone OS:CPU iPhone OS 7_0_3 like Mac OS X Browser:Safari-7.0 Engine:AppleWebKit-537.51.1 Bot:false Mobile:true",
400-
expectedOS: &OSInfo{"CPU iPhone OS 7_0_3 like Mac OS X", "iPhone OS", "7.0.3", "", "iPhone"},
400+
expectedOS: &OSInfo{"CPU iPhone OS 7_0_3 like Mac OS X", "iPhone OS", "7.0.3"},
401401
},
402402
{
403403
title: "iPhone",
@@ -428,7 +428,7 @@ var uastrings = []struct {
428428
title: "BlackBerry",
429429
ua: "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, Like Gecko) Version/6.0.0.141 Mobile Safari/534.1+",
430430
expected: "Mozilla:5.0 Platform:BlackBerry OS:BlackBerry 9800 Localization:en Browser:BlackBerry-6.0.0.141 Engine:AppleWebKit-534.1+ Bot:false Mobile:true",
431-
expectedOS: &OSInfo{"BlackBerry 9800", "BlackBerry", "9800", "en", "BlackBerry"},
431+
expectedOS: &OSInfo{"BlackBerry 9800", "BlackBerry", "9800"},
432432
},
433433
{
434434
title: "BB10",
@@ -439,7 +439,7 @@ var uastrings = []struct {
439439
title: "Ericsson",
440440
ua: "Mozilla/5.0 (SymbianOS/9.4; U; Series60/5.0 Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) Version/3.0 Safari/525",
441441
expected: "Mozilla:5.0 Platform:Symbian OS:SymbianOS/9.4 Browser:Symbian-3.0 Engine:AppleWebKit-525 Bot:false Mobile:true",
442-
expectedOS: &OSInfo{"SymbianOS/9.4", "SymbianOS", "9.4", "", "Symbian"},
442+
expectedOS: &OSInfo{"SymbianOS/9.4", "SymbianOS", "9.4"},
443443
},
444444
{
445445
title: "ChromeAndroid",
@@ -507,7 +507,7 @@ var uastrings = []struct {
507507
title: "Dalvik - Asus:T00Q",
508508
ua: "Dalvik/1.6.0 (Linux; U; Android 4.4.2; ASUS_T00Q Build/KVT49L)/CLDC-1.1",
509509
expected: "Mozilla:5.0 Platform:Linux OS:Android 4.4.2 Bot:false Mobile:true",
510-
expectedOS: &OSInfo{"Android 4.4.2", "Android", "4.4.2", "", "Linux"},
510+
expectedOS: &OSInfo{"Android 4.4.2", "Android", "4.4.2"},
511511
},
512512
{
513513
title: "Dalvik - W2430",

operating_systems.go

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ type OSInfo struct {
1717

1818
// Operating system version, e.g. 7 for Windows 7 or 10.8 for Max OS X Mountain Lion
1919
Version string
20-
21-
// Same as ua.Localization()
22-
Localization string
23-
24-
// Same as ua.Platform()
25-
Platform string
2620
}
2721

2822
// Normalize the name of the operating system. By now, this just
@@ -301,25 +295,13 @@ func (p *UserAgent) Localization() string {
301295
return p.localization
302296
}
303297

304-
// Returns combined information for the operating system.
305-
func (p *UserAgent) OSInfo() OSInfo {
306-
// Special case for iPhone weirdness
307-
os := strings.Replace(p.os, "like Mac OS X", "", 1)
308-
os = strings.Replace(os, "CPU", "", 1)
309-
os = strings.Trim(os, " ")
310-
311-
osSplit := strings.Split(os, " ")
312-
313-
// Special case for x64 edition of Windows
314-
if os == "Windows XP x64 Edition" {
315-
osSplit = osSplit[:len(osSplit)-2]
316-
}
317-
318-
var name, version string
298+
// Return OS name and version from a slice of strings created from the full name of the OS.
299+
func osName(osSplit []string) (name, version string) {
319300
if len(osSplit) == 1 {
320301
name = osSplit[0]
321302
version = ""
322303
} else {
304+
// Assume version is stored in the last part of the array.
323305
nameSplit := osSplit[:len(osSplit)-1]
324306
version = osSplit[len(osSplit)-1]
325307

@@ -338,6 +320,24 @@ func (p *UserAgent) OSInfo() OSInfo {
338320
version = ""
339321
}
340322
}
323+
return name, version
324+
}
325+
326+
// Returns combined information for the operating system.
327+
func (p *UserAgent) OSInfo() OSInfo {
328+
// Special case for iPhone weirdness
329+
os := strings.Replace(p.os, "like Mac OS X", "", 1)
330+
os = strings.Replace(os, "CPU", "", 1)
331+
os = strings.Trim(os, " ")
332+
333+
osSplit := strings.Split(os, " ")
334+
335+
// Special case for x64 edition of Windows
336+
if os == "Windows XP x64 Edition" {
337+
osSplit = osSplit[:len(osSplit)-2]
338+
}
339+
340+
name, version := osName(osSplit)
341341

342342
// Special case for names that contain a forward slash version separator.
343343
if strings.Contains(name, "/") {
@@ -350,10 +350,8 @@ func (p *UserAgent) OSInfo() OSInfo {
350350
version = strings.Replace(version, "_", ".", -1)
351351

352352
return OSInfo{
353-
FullName: p.os,
354-
Name: name,
355-
Version: version,
356-
Localization: p.localization,
357-
Platform: p.platform,
353+
FullName: p.os,
354+
Name: name,
355+
Version: version,
358356
}
359357
}

0 commit comments

Comments
 (0)