package main import ( "encoding/json" "io/ioutil" "path/filepath" "strings" "fagott.pw/grilist/models" ) const failMsg = "Offenbar war meine Annahme bzgl. der Traits nicht ganz korrekt" type ACDv2Reader struct{} type ACDv2NameValue struct { Name string `json:"name"` Value string `json:"value"` } type ACDv2Gril struct { Name struct { Romaji string `json:"romaji"` Japanese string `json:"japanese"` Aliases []string `json:"aliases"` } `json:"name"` Image struct { Thumbnail string `json:"thumb"` Full string `json:"full"` } `json:"image"` Tags []struct { ID int `json:"id"` Name string `json:"name"` } `json:"tags"` Traits struct { Official []ACDv2NameValue `json:"official"` Indexed []ACDv2NameValue `json:"indexed"` } `json:"traits"` Assignments []int `json:"assignments"` CharsWithSimilarTraits []int `json:"chars_similar_traits"` Extra []ACDv2NameValue `json:"extra"` Role string `json:"role"` } func (r *ACDv2Reader) Read(path string) WrappedGril { g := WrappedGril{} g.Gril = models.Gril{} data, err := ioutil.ReadFile(path) ifErrExit(err) var jObj ACDv2Gril err = json.Unmarshal(data, &jObj) ifErrExit(err) g.Image = jObj.Image.Full g.Thumb = jObj.Image.Thumbnail g.Gril.ID = toInt(strings.TrimSuffix(filepath.Base(path), ".json")) g.Gril.KanjiName = jObj.Name.Japanese g.Gril.RomajiName = jObj.Name.Romaji g.Gril.OtherNames = jObj.Name.Aliases g.Gril.Tags = make([]string, 0) for _, v := range jObj.Tags { g.Gril.Tags = append(g.Gril.Tags, v.Name) } g.Gril.Traits = make([]models.Trait, 0) if len(jObj.Traits.Official) != len(jObj.Traits.Indexed) { panic(failMsg) } for i := 0; i < len(jObj.Traits.Official); i++ { if jObj.Traits.Official[i].Name != jObj.Traits.Indexed[i].Name { panic(failMsg) } t := models.Trait{ Name: jObj.Traits.Official[i].Name, Value: jObj.Traits.Indexed[i].Value, OfficialValue: jObj.Traits.Official[i].Value, } g.Gril.Traits = append(g.Gril.Traits, t) } g.Gril.Series = make([]models.Series, 0) for _, v := range jObj.Assignments { s := models.Series{ ID: v, } g.Gril.Series = append(g.Gril.Series, s) } return g } func (r *ACDv2Reader) ID() int { return int(models.DataSourceACD) }