/
proficiency.go
64 lines (53 loc) · 1.76 KB
/
proficiency.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
63
64
package popper
import (
"context"
"strings"
"github.com/ecshreve/dndgen/ent"
"github.com/samsarahq/go/oops"
log "github.com/sirupsen/logrus"
)
type ProficiencyWrapper struct {
Indx string `json:"index"`
Name string `json:"name"`
Reference struct {
Indx string `json:"index"`
Url string `json:"url"`
} `json:"reference"`
Classes []*ent.Class `json:"classes"`
Races []*ent.Race `json:"races"`
}
// ToEnt converts the ProficiencyWrapper to an ent.Proficiency.
func (p *ProficiencyWrapper) ToEnt() *ent.Proficiency {
return &ent.Proficiency{
Indx: p.Indx,
Name: p.Name,
ProficiencyCategory: strings.Replace(strings.Split(p.Reference.Url, "/")[2], "-", "_", -1),
}
}
// PopulateProficiency populates the Proficiency entities from the JSON data files.
func (p *Popper) PopulateProficiency(ctx context.Context) ([]*ent.Proficiency, error) {
fpath := "data/Proficiency.json"
var v []ProficiencyWrapper
if err := LoadJSONFile(fpath, &v); err != nil {
return nil, oops.Wrapf(err, "unable to load JSON file %s", fpath)
}
creates := make([]*ent.ProficiencyCreate, len(v))
for i, vv := range v {
creates[i] = p.Client.Proficiency.Create().SetProficiency(&ent.Proficiency{
Indx: vv.Indx,
Name: vv.Name,
ProficiencyCategory: strings.Replace(strings.Split(vv.Reference.Url, "/")[2], "-", "_", -1),
})
}
created, err := p.Client.Proficiency.CreateBulk(creates...).Save(ctx)
if err != nil {
return nil, oops.Wrapf(err, "unable to save Proficiency entities")
}
log.Infof("created %d entities for type Proficiency", len(created))
p.PopulateProficiencyEdges(ctx, created, v)
for _, c := range created {
p.IdToIndx[c.ID] = c.Indx
p.IndxToId[c.Indx] = c.ID
}
return created, nil
}