From 3d526f9198c7ad1f1f744d1b788007fc62c9d46c Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 4 Jun 2019 12:16:27 +0900 Subject: [PATCH] Cleanup data types --- CONTRIBUTING.md | 2 +- arn/Anime.go | 6 +- arn/AnimeImage.go | 9 - arn/Character.go | 2 +- arn/CharacterImage.go | 3 - arn/Group.go | 2 +- arn/GroupImage.go | 3 - arn/Image.go | 10 + arn/{AnimeTitle.go => MediaTitle.go} | 6 +- arn/Person.go | 6 +- arn/PersonImage.go | 4 - docs/code-style.md | 178 ------------------ jobs/kitsu-import-anime/kitsu-import-anime.go | 2 +- 13 files changed, 23 insertions(+), 210 deletions(-) create mode 100644 arn/Image.go rename arn/{AnimeTitle.go => MediaTitle.go} (86%) delete mode 100644 arn/PersonImage.go delete mode 100644 docs/code-style.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b0dda75e..4f63c228 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -5,7 +5,7 @@ - [Installation](README.md#installation) - [Task for new contributors](docs/new-contributor-task.md) - [Issues](https://github.com/animenotifier/notify.moe/projects/10) -- [Code Style](docs/code-style.md) +- [Code Style](https://github.com/akyoto/quality/blob/master/STYLE.md) ## Communication diff --git a/arn/Anime.go b/arn/Anime.go index bbfd6b7c..acd5d007 100644 --- a/arn/Anime.go +++ b/arn/Anime.go @@ -65,7 +65,7 @@ func init() { // Anime represents an anime. type Anime struct { Type string `json:"type" editable:"true" datalist:"anime-types"` - Title *AnimeTitle `json:"title" editable:"true"` + Title *MediaTitle `json:"title" editable:"true"` Summary string `json:"summary" editable:"true" type:"textarea"` Status string `json:"status" editable:"true" datalist:"anime-status"` Genres []string `json:"genres" editable:"true"` @@ -74,7 +74,7 @@ type Anime struct { EpisodeCount int `json:"episodeCount" editable:"true"` EpisodeLength int `json:"episodeLength" editable:"true"` Source string `json:"source" editable:"true" datalist:"anime-sources"` - Image AnimeImage `json:"image"` + Image Image `json:"image"` FirstChannel string `json:"firstChannel"` Rating *AnimeRating `json:"rating"` Popularity *AnimePopularity `json:"popularity"` @@ -107,7 +107,7 @@ func NewAnime() *Anime { hasID: hasID{ ID: GenerateID("Anime"), }, - Title: &AnimeTitle{}, + Title: &MediaTitle{}, Rating: &AnimeRating{}, Popularity: &AnimePopularity{}, Trailers: []*ExternalMedia{}, diff --git a/arn/AnimeImage.go b/arn/AnimeImage.go index ae4378b7..39e834f0 100644 --- a/arn/AnimeImage.go +++ b/arn/AnimeImage.go @@ -157,15 +157,6 @@ var animeImageOutputsHighDPI = []imageserver.Output{ }, } -// AnimeImage ... -type AnimeImage struct { - Extension string `json:"extension"` - Width int `json:"width"` - Height int `json:"height"` - AverageColor HSLColor `json:"averageColor"` - LastModified int64 `json:"lastModified"` -} - // SetImageBytes accepts a byte buffer that represents an image file and updates the anime image. func (anime *Anime) SetImageBytes(data []byte) error { // Decode diff --git a/arn/Character.go b/arn/Character.go index 497046fb..0c775a49 100644 --- a/arn/Character.go +++ b/arn/Character.go @@ -11,7 +11,7 @@ import ( // Character represents an anime or manga character. type Character struct { Name CharacterName `json:"name" editable:"true"` - Image CharacterImage `json:"image"` + Image Image `json:"image"` MainQuoteID string `json:"mainQuoteId" editable:"true"` Description string `json:"description" editable:"true" type:"textarea"` Spoilers []Spoiler `json:"spoilers" editable:"true"` diff --git a/arn/CharacterImage.go b/arn/CharacterImage.go index 942dd093..bf165fc2 100644 --- a/arn/CharacterImage.go +++ b/arn/CharacterImage.go @@ -148,9 +148,6 @@ var characterImageOutputsHighDPI = []imageserver.Output{ }, } -// CharacterImage ... -type CharacterImage AnimeImage - // SetImageBytes accepts a byte buffer that represents an image file and updates the character image. func (character *Character) SetImageBytes(data []byte) error { // Decode diff --git a/arn/Group.go b/arn/Group.go index 45472370..a9703476 100644 --- a/arn/Group.go +++ b/arn/Group.go @@ -15,7 +15,7 @@ import ( type Group struct { Name string `json:"name" editable:"true"` Tagline string `json:"tagline" editable:"true"` - Image GroupImage `json:"image"` + Image Image `json:"image"` Description string `json:"description" editable:"true" type:"textarea"` Rules string `json:"rules" editable:"true" type:"textarea"` Restricted bool `json:"restricted" editable:"true" tooltip:"Restricted groups can only be joined with the founder's permission."` diff --git a/arn/GroupImage.go b/arn/GroupImage.go index c3e16f95..c8185045 100644 --- a/arn/GroupImage.go +++ b/arn/GroupImage.go @@ -116,9 +116,6 @@ var groupImageOutputsHighDPI = []imageserver.Output{ }, } -// GroupImage ... -type GroupImage AnimeImage - // SetImageBytes accepts a byte buffer that represents an image file and updates the group image. func (group *Group) SetImageBytes(data []byte) error { // Decode diff --git a/arn/Image.go b/arn/Image.go new file mode 100644 index 00000000..8869b9cf --- /dev/null +++ b/arn/Image.go @@ -0,0 +1,10 @@ +package arn + +// Image represents an image with meta data. +type Image struct { + Extension string `json:"extension"` + Width int `json:"width"` + Height int `json:"height"` + AverageColor HSLColor `json:"averageColor"` + LastModified int64 `json:"lastModified"` +} diff --git a/arn/AnimeTitle.go b/arn/MediaTitle.go similarity index 86% rename from arn/AnimeTitle.go rename to arn/MediaTitle.go index c9839080..a6cb0931 100644 --- a/arn/AnimeTitle.go +++ b/arn/MediaTitle.go @@ -1,7 +1,7 @@ package arn -// AnimeTitle ... -type AnimeTitle struct { +// MediaTitle represents a title for any kind of media. +type MediaTitle struct { Canonical string `json:"canonical" editable:"true"` Romaji string `json:"romaji" editable:"true"` English string `json:"english" editable:"true"` @@ -11,7 +11,7 @@ type AnimeTitle struct { } // ByUser returns the preferred title for the given user. -func (title *AnimeTitle) ByUser(user *User) string { +func (title *MediaTitle) ByUser(user *User) string { if user == nil { return title.Canonical } diff --git a/arn/Person.go b/arn/Person.go index b59eb8c3..6ea25379 100644 --- a/arn/Person.go +++ b/arn/Person.go @@ -10,8 +10,8 @@ import ( // Person represents a person in real life. type Person struct { - Name PersonName `json:"name" editable:"true"` - Image PersonImage `json:"image"` + Name PersonName `json:"name" editable:"true"` + Image Image `json:"image"` hasID hasPosts @@ -33,7 +33,7 @@ func NewPerson() *Person { } } -// Link ... +// Link returns the path to the person. func (person *Person) Link() string { return "/person/" + person.ID } diff --git a/arn/PersonImage.go b/arn/PersonImage.go deleted file mode 100644 index 8fa516da..00000000 --- a/arn/PersonImage.go +++ /dev/null @@ -1,4 +0,0 @@ -package arn - -// PersonImage ... -type PersonImage CharacterImage diff --git a/docs/code-style.md b/docs/code-style.md deleted file mode 100644 index 8da7f8d4..00000000 --- a/docs/code-style.md +++ /dev/null @@ -1,178 +0,0 @@ -# Code Style - -This document is only meant to teach you the code style used in this project and will not explain *why* this coding style is used. - -* [Tabs vs Spaces](#tabs-vs-spaces) -* [Empty line between blocks and statements](#empty-line-between-blocks-and-statements) -* [Empty line between commented blocks](#empty-line-between-commented-blocks) -* [Variable names](#variable-names) -* [Types at the top](#types-at-the-top) -* [Private fields at the end of a struct](#private-fields-at-the-end-of-a-struct) -* [Don't comment out outdated code](#dont-comment-out-outdated-code) -* [Comments start with space and uppercase](#comments-start-with-space-and-uppercase) -* [Package names](#package-names) -* [Use gofmt](#use-gofmt) -* [Code editor](#code-editor) - -## Tabs vs Spaces - -Use tabs for indentation and spaces for alignment: - -```go -type AnimeTitle struct { - Romaji string - English string - Japanese string - Canonical string - Synonyms []string -} -``` - -## Empty line between blocks and statements - -Bad: - -```go -func() { - if true { - // Block 1 - } - if true { - // Block 2 - } - doSomething() - doSomething() - if true { - // Block 3 - } -} -``` - -Good: - -```go -func() { - if true { - // Block 1 - } - - if true { - // Block 2 - } - - doSomething() - doSomething() - - if true { - // Block 3 - } -} -``` - -## Empty line between commented blocks - -A commented block is a sequence of lines preceded by a descriptive comment. Commented blocks should be treated as normal blocks and therefore require newlines separating them from other blocks. - -Bad: - -```go -func(a int, b int) int { - // Add one to a and b - a++ - b++ - // Calculate c as the sum of a and b - c = a + b - // Return c squared - return c * c -} -``` - -Good: - -```go -func(a int, b int) int { - // Add one to a and b - a++ - b++ - - // Calculate c as the sum of a and b - c = a + b - - // Return c squared - return c * c -} -``` - -## Variable names - -Variables are written in `camelCase` and should clearly state what they contain, preferably with no abbreviations. Common short names like `id` and `url` are allowed. - -Iterator variables in loops are an exception to this rule and can be 1-letter, non-significant variable names, usually `i` and `j`. `index` is also quite common. - -## Early returns - -Do not wrap a whole function in 1 if-block to check parameters. Use early returns. - -Bad: - -```go -func DoSomething(a string, b string) { - if a != "" && b != "" { - doIt(a, b) - } -} -``` - -Good: - -```go -func DoSomething(a string, b string) { - if a == "" || b == "" { - return - } - - doIt(a, b) -} -``` - -## Types at the top - -`type` definitions should be placed at the very top of your files. Variables, constants, interface implementation assertions and the `package` statement are the only constructs allowed above `type` definitions. - -## Private fields at the end of a struct - -This is not an absolute rule but try to keep private fields at the end of a struct. - -```go -type MyType struct { - PublicA string - PublicB string - PublicC string - - privateA int -} -``` - -## Don't comment out outdated code - -You should delete outdated code instead of commenting it out. Comments should be used for explanation of existing code and outdated code is saved in the git history anyway if you ever need it (in most cases outdated code is never re-used). - -## Comments start with space and uppercase - -Example: - -```go -// This comment starts with a space and an uppercase letter. -``` - -## Package names - -Package names should be short lowercase identifiers and tests should be written using the black box pattern. Black box testing can be enabled by adding the suffix `_test` to the package names in `*_test.go` files. It will enable you to test your library like it would be used by another developer, without internal access to private variables. - -## Use gofmt - -Your IDE should automatically call `gofmt` to format your code every time you save the file. - -## Code editor - -It is highly recommended to use [Visual Studio Code](https://code.visualstudio.com/) as it has an excellent Go plugin and the repository includes workspace settings to get you started quickly. \ No newline at end of file diff --git a/jobs/kitsu-import-anime/kitsu-import-anime.go b/jobs/kitsu-import-anime/kitsu-import-anime.go index edc33955..16d5e1b1 100644 --- a/jobs/kitsu-import-anime/kitsu-import-anime.go +++ b/jobs/kitsu-import-anime/kitsu-import-anime.go @@ -44,7 +44,7 @@ func sync(anime *kitsu.Anime) { // if err != nil { // if strings.Contains(err.Error(), "not found") { // anime = &arn.Anime{ -// Title: &arn.AnimeTitle{}, +// Title: &arn.MediaTitle{}, // } // } else { // panic(err)