Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit 55f6794

Browse files
committed
Add asset parser
0 parents  commit 55f6794

File tree

4 files changed

+220
-0
lines changed

4 files changed

+220
-0
lines changed

main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/evepraisal/go-evepraisal/parsers"
7+
)
8+
9+
func main() {
10+
log.Println(parsers.ParseAssets([]string{"Sleeper Data Library\t1.080\tSleeper Components\t\t\t10.80 m3"}))
11+
}

parsers/assets.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package parsers
2+
3+
import (
4+
"log"
5+
"regexp"
6+
"strings"
7+
)
8+
9+
type AssetRow struct {
10+
Name string
11+
Quantity int64
12+
Group string
13+
Category string
14+
Size string
15+
Slot string
16+
Volume float64
17+
MetaLevel string
18+
TechLevel string
19+
}
20+
21+
var assetList = regexp.MustCompile(strings.Join([]string{
22+
`^([\S\ ]*)`, // name
23+
`\t([\d,'\.]*)`, // quantity
24+
`(\t([\S ]*))?`, // group
25+
`(\t([\S ]*))?`, // category
26+
`(\t(XLarge|Large|Medium|Small|))?`, // size
27+
`(\t(High|Medium|Low|Rigs|[\d ]*))?`, // slot
28+
`(\t([\d ,\.]*) m3)?`, // volume
29+
`(\t([\d]+|))?`, // meta level
30+
`(\t([\d]+|))?$`, // tech level
31+
}, ""))
32+
33+
func ParseAssets(lines []string) ([]IResult, []string) {
34+
var matches []IResult
35+
var rest []string
36+
for _, line := range lines {
37+
match := assetList.FindStringSubmatch(line)
38+
log.Printf("%#v", match)
39+
if len(match) == 0 {
40+
rest = append(rest, line)
41+
} else {
42+
matches = append(matches,
43+
AssetRow{
44+
match[1],
45+
ToInt(match[2]),
46+
match[4],
47+
match[6],
48+
match[8],
49+
match[10],
50+
ToFloat64(match[12]),
51+
match[14],
52+
match[16],
53+
})
54+
}
55+
}
56+
return matches, rest
57+
}

parsers/parser_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package parsers
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
type Case struct {
12+
Description string
13+
Input string
14+
Expected []IResult
15+
ExpectedRest []string
16+
}
17+
18+
var empty []string
19+
20+
var assetListTestCases = []Case{
21+
{
22+
"Simple",
23+
`Hurricane 1 Combat Battlecruiser`,
24+
[]IResult{AssetRow{Name: "Hurricane", Group: "Combat Battlecruiser", Quantity: 1}},
25+
empty,
26+
}, {
27+
"Typical",
28+
`720mm Gallium Cannon 1 Projectile Weapon Medium High 10 m3
29+
Damage Control II 1 Damage Control Low 5 m3
30+
Experimental 10MN Microwarpdrive I 1 Propulsion Module Medium 10 m3`,
31+
[]IResult{
32+
AssetRow{Name: "720mm Gallium Cannon", Quantity: 1, Group: "Projectile Weapon", Category: "Medium", Slot: "High", Volume: 10},
33+
AssetRow{Name: "Damage Control II", Quantity: 1, Group: "Damage Control", Slot: "Low", Volume: 5},
34+
AssetRow{Name: "Experimental 10MN Microwarpdrive I", Quantity: 1, Group: "Propulsion Module", Size: "Medium", Volume: 10}},
35+
empty,
36+
}, {
37+
"Full",
38+
`200mm AutoCannon I 1 Projectile Weapon Module Small High 5 m3 1
39+
10MN Afterburner II 1 Propulsion Module Module Medium 5 m3 5 2
40+
Warrior II 9`,
41+
[]IResult{
42+
AssetRow{Name: "200mm AutoCannon I", Quantity: 1, Group: "Projectile Weapon", Category: "Module", Size: "Small", Slot: "High", MetaLevel: "1", Volume: 5},
43+
AssetRow{Name: "10MN Afterburner II", Quantity: 1, Group: "Propulsion Module", Category: "Module", Size: "Medium", MetaLevel: "5", TechLevel: "2", Volume: 5},
44+
AssetRow{Name: "Warrior II", Quantity: 9}},
45+
empty,
46+
}, {
47+
"With volumes",
48+
`Sleeper Data Library 1.080 Sleeper Components 10.80 m3`,
49+
[]IResult{AssetRow{Name: "Sleeper Data Library", Quantity: 1, Group: "Sleeper Components", Volume: 10.80}},
50+
empty,
51+
},
52+
}
53+
54+
// ASSET_TABLE.add_test(u'''
55+
// Sleeper Data Library\t1\xc2\xa0080\tSleeper Components\t\t\t10.80 m3
56+
// ''', ([{'category': '',
57+
// 'group': 'Sleeper Components',
58+
// 'meta_level': None,
59+
// 'name': 'Sleeper Data Library',
60+
// 'quantity': 1080,
61+
// 'size': '',
62+
// 'slot': None,
63+
// 'tech_level': None,
64+
// 'volume': '10.80 m3'}], []))
65+
// ASSET_TABLE.add_test('''
66+
// Sleeper Data Library\t1,080\tSleeper Components\t\t\t10.80 m3
67+
// ''', ([{'category': '',
68+
// 'group': 'Sleeper Components',
69+
// 'meta_level': None,
70+
// 'name': 'Sleeper Data Library',
71+
// 'quantity': 1080,
72+
// 'size': '',
73+
// 'slot': None,
74+
// 'tech_level': None,
75+
// 'volume': '10.80 m3'}], []))
76+
// ASSET_TABLE.add_test('''
77+
// Amarr Dreadnought\t1\tSpaceship Command\tSkill\t\t\t0.01 m3\t\t
78+
// ''', ([{'category': 'Skill',
79+
// 'slot': '',
80+
// 'group': 'Spaceship Command',
81+
// 'name': 'Amarr Dreadnought',
82+
// 'volume': '0.01 m3',
83+
// 'size': '',
84+
// 'tech_level': '',
85+
// 'meta_level': '',
86+
// 'quantity': 1}], []))
87+
// ASSET_TABLE.add_test('''
88+
// Quafe Zero\t12\tBooster\tImplant\t\t1 \t12 m3\t\t
89+
// ''', ([{'category': 'Implant',
90+
// 'slot': '1 ',
91+
// 'group': 'Booster',
92+
// 'name': 'Quafe Zero',
93+
// 'volume': '12 m3',
94+
// 'size': '',
95+
// 'tech_level': '',
96+
// 'meta_level': '',
97+
// 'quantity': 12}], []))
98+
// ASSET_TABLE.add_test(
99+
// u"Antimatter Charge M\t100\xc2\xa0000\tHybrid Charge\tMedium\t\t"
100+
// u"1\xc2\xa0250 m3",
101+
// ([{'category': 'Medium',
102+
// 'group': 'Hybrid Charge',
103+
// 'meta_level': None,
104+
// 'name': 'Antimatter Charge M',
105+
// 'quantity': 100000,
106+
// 'size': '',
107+
// 'slot': None,
108+
// 'tech_level': None,
109+
// 'volume': '1250 m3'}], []))
110+
// ASSET_TABLE.add_test("Hurricane\t12'000\tCombat Battlecruiser\t\t\t15,000 m3",
111+
// ([{'category': '',
112+
// 'slot': None,
113+
// 'group': 'Combat Battlecruiser',
114+
// 'name': 'Hurricane',
115+
// 'volume': '15,000 m3',
116+
// 'size': '',
117+
// 'tech_level': None,
118+
// 'meta_level': None,
119+
// 'quantity': 12000}], []))
120+
121+
func TestParsers(rt *testing.T) {
122+
for _, c := range assetListTestCases {
123+
rt.Run(c.Description, func(t *testing.T) {
124+
fmt.Println(c.Input)
125+
result, rest := ParseAssets(strings.Split(c.Input, "\n"))
126+
assert.Equal(t, c.Expected, result, "results should be the same")
127+
assert.Equal(t, c.ExpectedRest, rest, "the rest should be the same")
128+
})
129+
}
130+
}

parsers/utils.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package parsers
2+
3+
import "strconv"
4+
5+
type IResult interface {
6+
}
7+
8+
func ToInt(s string) int64 {
9+
i, err := strconv.ParseInt(s, 10, 64)
10+
if err != nil {
11+
return int64(ToFloat64(s))
12+
}
13+
return i
14+
}
15+
16+
func ToFloat64(s string) float64 {
17+
f, err := strconv.ParseFloat(s, 64)
18+
if err != nil {
19+
return 0.0
20+
}
21+
return f
22+
}

0 commit comments

Comments
 (0)