From da3c064496f91f4d0248ff2ae3da7e12ad42df1d Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 27 May 2019 18:08:42 +0900 Subject: [PATCH] Improved reflection in editform --- go.mod | 7 ++++--- go.sum | 16 ++++++---------- pages/editor/diffs.go | 15 ++++++++------- utils/HashStrings.go | 24 ------------------------ utils/HashStringsNoOrder.go | 14 ++++++++++++++ utils/editform/editform.go | 15 ++++++++++++++- 6 files changed, 46 insertions(+), 45 deletions(-) delete mode 100644 utils/HashStrings.go create mode 100644 utils/HashStringsNoOrder.go diff --git a/go.mod b/go.mod index b1834f03..6514a35a 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.12 require ( cloud.google.com/go v0.39.0 // indirect - github.com/OneOfOne/xxhash v1.2.5 github.com/StackExchange/wmi v0.0.0-20181212234831-e0a55b97c705 // indirect github.com/aerogo/aero v1.2.5 github.com/aerogo/api v0.1.7 @@ -22,9 +21,10 @@ require ( github.com/aerogo/sitemap v0.1.2 github.com/akyoto/cache v1.0.2 github.com/akyoto/color v1.8.5 - github.com/akyoto/stringutils v0.2.0 + github.com/akyoto/hash v0.3.3 + github.com/akyoto/stringutils v0.2.1 github.com/animenotifier/anilist v0.2.3 - github.com/animenotifier/arn v1.1.24 + github.com/animenotifier/arn v1.1.25 github.com/animenotifier/kitsu v0.2.3 github.com/animenotifier/mal v0.2.3 github.com/animenotifier/shoboi v0.2.3 @@ -48,6 +48,7 @@ require ( github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a // indirect github.com/stretchr/testify v1.3.0 golang.org/x/oauth2 v0.0.0-20190523182746-aaccbc9213b0 + golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect golang.org/x/text v0.3.2 // indirect google.golang.org/appengine v1.6.0 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect diff --git a/go.sum b/go.sum index f501933a..8e5326f6 100644 --- a/go.sum +++ b/go.sum @@ -19,8 +19,6 @@ github.com/aerogo/aero v1.1.9 h1:EB+oTljSIfQENZTVyvnZ24Pb2UsV+Ows04/HfzOJ/3c= github.com/aerogo/aero v1.1.9/go.mod h1:MvHPJcXZmZUu5mh4q6j5n3DijKmAMY6puhzCH1w22Vw= github.com/aerogo/aero v1.1.13 h1:e+FXHaSRZ2a/xrLwF1Yeujh5yHBovdLfHxJYtZvPpWw= github.com/aerogo/aero v1.1.13/go.mod h1:jcHCf+a3vExpo1SKRPrmkspO9VWiDZHC5/ITss/Sz6Y= -github.com/aerogo/aero v1.2.1 h1:oCDufiQua/URRWGs1cTAR8eroeRJSjakaS7zoTuI0Bc= -github.com/aerogo/aero v1.2.1/go.mod h1:NiTEzYsRlHY9vmzw9tKJqRiDh36LVVZba405l5HM0XE= github.com/aerogo/aero v1.2.5 h1:xn1POlFP8gWUDv99QqetijOsghgHt5WXAEXiqhmRQbQ= github.com/aerogo/aero v1.2.5/go.mod h1:jRXI34hJL3iwH+llwLLnMPPHr9tFgLfl0JQhaH+8eJQ= github.com/aerogo/api v0.1.7 h1:2cEOUlPvlRnLo6A0xn8+UpmluWqRoYEGn0Ik4kxmUEI= @@ -71,8 +69,6 @@ github.com/aerogo/nano v0.2.0 h1:qcCdCsAtN1Qpw8DhZQXiLVJYsjmpQMmZ7TPFD2GkwVw= github.com/aerogo/nano v0.2.0/go.mod h1:tbR/fVGJb2rRFdPp9+D0INJgkhzQ+9XKefLTI11xNFU= github.com/aerogo/nano v0.2.1 h1:YDGytzYoswid2x8cR5yAc9TmjgR+wEMF8+HowR1UCKI= github.com/aerogo/nano v0.2.1/go.mod h1:1xgPREJH6aW5sE91AwYkvxMDg5YCbAaIYpAL7T1+JgA= -github.com/aerogo/nano v0.3.1 h1:tRlAbATkFqQ9yTxD/L8cFs16TGVVP8fMo+NYVoTYgCU= -github.com/aerogo/nano v0.3.1/go.mod h1:o86DA68cpXx5inh8tygTJBF+8sXQ0eoOm36NHIN86YI= github.com/aerogo/nano v0.3.2 h1:1JWaZX8e+dtKZuPWC7Ls1AuzN33auCw7hPjZ8JCjjjE= github.com/aerogo/nano v0.3.2/go.mod h1:o86DA68cpXx5inh8tygTJBF+8sXQ0eoOm36NHIN86YI= github.com/aerogo/pack v0.5.0 h1:0BQVGNaPTIV4fQ5YG17eqOoe30aqpZp1NXCZxxRXxrA= @@ -123,6 +119,8 @@ github.com/akyoto/go-matroska v0.1.1 h1:HgoCAkeWrGjYr0FZr3yCzAIkXuOGRiVil7Ul329l github.com/akyoto/go-matroska v0.1.1/go.mod h1:x+GUVwyby6HN/MKKNP4BvGqP9VrHuEznfBf288gehek= github.com/akyoto/hash v0.3.2 h1:T6nwsswtzXUjk5pBOLSDdj/8m9hcZzVzj35qnfG24aA= github.com/akyoto/hash v0.3.2/go.mod h1:t6CqUCKqLvd1QfbNSt9yfv9BiOY+qPsZABimD86fNPY= +github.com/akyoto/hash v0.3.3 h1:zOQ71sRCRU56beF148dWUaCmWWpuLmi3dBd1Jiy8vHE= +github.com/akyoto/hash v0.3.3/go.mod h1:uPmnZyhBJIyLON8V9LNi0CcqtwYaH2RiKLFQg67fwq0= github.com/akyoto/ignore v1.0.2 h1:A9hy/aUjaiKfjci0OPvwvme5F0e3PHrN9bjNn9oASkM= github.com/akyoto/ignore v1.0.2/go.mod h1:GhkaVB2bVMq7KuV9AkB1U6FGWZ7mTpuXeKVQNEWVUc0= github.com/akyoto/imageserver v0.3.6 h1:Sxcbgo45Lh7afcSmcU8OS49VYbqh4kE3DK0Lxuuxf74= @@ -131,14 +129,16 @@ github.com/akyoto/stringutils v0.1.0 h1:MuRXgxETSPgkrT+j3L9/oEqHs2/yW/gr2ioq3j/5 github.com/akyoto/stringutils v0.1.0/go.mod h1:a83vMdYtgvZD3nuWJIW5kzbrjv5dHH3Qg6ucbF1Z/k4= github.com/akyoto/stringutils v0.2.0 h1:86gMW/31LO7zc8mGGWB4gLqyFzrNHMZYyj6ebSQbpH4= github.com/akyoto/stringutils v0.2.0/go.mod h1:EigXf5ZaULP6f5CNjqIIo0fjDHbRe8P4BQwlqCsvi9k= +github.com/akyoto/stringutils v0.2.1 h1:4St8vcRnNFuCNSMEJ34TeLb7/FfsvZaSU8/Scr6Zymg= +github.com/akyoto/stringutils v0.2.1/go.mod h1:BUYzDYA2CZ7bqDZKpAQMUJOG9mCCOw4qd4JM3770bI8= github.com/akyoto/webpush-go v0.1.2 h1:Ufg8Al6xyyaTmdlLkfOGDEapVClymzJl/Whu8pOydjM= github.com/akyoto/webpush-go v0.1.2/go.mod h1:IxZu6lYe/IQ+jNFEcZjwp6cWyU/KsnARuLZKvM9SfOk= github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o= github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/animenotifier/anilist v0.2.3 h1:409h1m4m59EBTQHc/F2U5PGY3lIWlvD/kRXxY1oTl5Q= github.com/animenotifier/anilist v0.2.3/go.mod h1:WmivLHBTIs+zmqENjiVXH66laTYB8vT5d+8q1yzLX9I= -github.com/animenotifier/arn v1.1.24 h1:34MqIM02eAyWS5wZcFxNZlj6g6hqhavzpgHX9CtZU2k= -github.com/animenotifier/arn v1.1.24/go.mod h1:k1uAd27stpU0vN0N/DlEu6/oC9oZ/8Y4gZ2xs+YgB1Q= +github.com/animenotifier/arn v1.1.25 h1:RyAMLdOVQrGto5A+RSLgs/OrnTy5WSyql559YkyPvUI= +github.com/animenotifier/arn v1.1.25/go.mod h1:qf9Uq5lsw3uIgFFrfOSu1L9hjtHer9VcmgIWWcuN/Jo= github.com/animenotifier/ffxiv v0.2.1 h1:gV5h47skizAWLJQb+M3CmExy1hlqDuKmNxkOpn3JwF0= github.com/animenotifier/ffxiv v0.2.1/go.mod h1:9p0z9iQIT8nIlwH4xHUvdo0qFvJ4pVnFbBQ0G/JiY0k= github.com/animenotifier/japanese v0.2.3 h1:fGX3CcX5lGzRC+JkokDCwJqRniPOmM44FLm7aqdnOEo= @@ -337,12 +337,8 @@ golang.org/x/sys v0.0.0-20190516014833-cab07311ab81/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190516102723-cedb8e16d18a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190516110030-61b9204099cb h1:k07iPOt0d6nEnwXF+kHB+iEg+WSuKe/SOQuFM2QoD+E= golang.org/x/sys v0.0.0-20190516110030-61b9204099cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190522044717-8097e1b27ff5 h1:f005F/Jl5JLP036x7QIvUVhNTqxvSYwFIiyOh2q12iU= -golang.org/x/sys v0.0.0-20190522044717-8097e1b27ff5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190523142557-0e01d883c5c5 h1:sM3evRHxE/1RuMe1FYAL3j7C7fUfIjkbE+NiDAYUF8U= golang.org/x/sys v0.0.0-20190523142557-0e01d883c5c5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190524152521-dbbf3f1254d4 h1:VSJ45BzqrVgR4clSx415y1rHH7QAGhGt71J0ZmhLYrc= -golang.org/x/sys v0.0.0-20190524152521-dbbf3f1254d4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190526052359-791d8a0f4d09 h1:IlD35wZE03o2qJy2o37WIskL33b7PT6cHdGnE8bieZs= golang.org/x/sys v0.0.0-20190526052359-791d8a0f4d09/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= diff --git a/pages/editor/diffs.go b/pages/editor/diffs.go index dc843f48..e064c269 100644 --- a/pages/editor/diffs.go +++ b/pages/editor/diffs.go @@ -1,6 +1,7 @@ package editor import ( + "github.com/akyoto/hash" "github.com/animenotifier/arn" "github.com/animenotifier/mal" "github.com/animenotifier/notify.moe/utils" @@ -13,7 +14,7 @@ func diffTitles(anime *arn.Anime, malAnime *mal.Anime) []animediff.Difference { // Canonical title if anime.Title.Canonical != malAnime.Title { - hash := utils.HashString(malAnime.Title) + hash := hash.String(malAnime.Title) if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "CanonicalTitle", hash) { differences = append(differences, &animediff.CanonicalTitle{ @@ -26,7 +27,7 @@ func diffTitles(anime *arn.Anime, malAnime *mal.Anime) []animediff.Difference { // Japanese title if anime.Title.Japanese != malAnime.JapaneseTitle { - hash := utils.HashString(malAnime.JapaneseTitle) + hash := hash.String(malAnime.JapaneseTitle) if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "JapaneseTitle", hash) { differences = append(differences, &animediff.JapaneseTitle{ @@ -39,7 +40,7 @@ func diffTitles(anime *arn.Anime, malAnime *mal.Anime) []animediff.Difference { // Romaji title if anime.Title.Romaji != malAnime.Title { - hash := utils.HashString(malAnime.Title) + hash := hash.String(malAnime.Title) if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "RomajiTitle", hash) { differences = append(differences, &animediff.RomajiTitle{ @@ -59,7 +60,7 @@ func diffDates(anime *arn.Anime, malAnime *mal.Anime) []animediff.Difference { // Airing start date if anime.StartDate != malAnime.StartDate && malAnime.StartDate != "" { - hash := utils.HashString(malAnime.StartDate) + hash := hash.String(malAnime.StartDate) if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "StartDate", hash) { differences = append(differences, &animediff.StartDate{ @@ -72,7 +73,7 @@ func diffDates(anime *arn.Anime, malAnime *mal.Anime) []animediff.Difference { // Airing end date if anime.EndDate != malAnime.EndDate && malAnime.EndDate != "" { - hash := utils.HashString(malAnime.EndDate) + hash := hash.String(malAnime.EndDate) if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "EndDate", hash) { differences = append(differences, &animediff.EndDate{ @@ -112,7 +113,7 @@ func diffStatus(anime *arn.Anime, malAnime *mal.Anime) []animediff.Difference { // Status if anime.Status != malAnime.Status { - hash := utils.HashString(malAnime.Status) + hash := hash.String(malAnime.Status) if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "Status", hash) { differences = append(differences, &animediff.Status{ @@ -132,7 +133,7 @@ func diffSynopsis(anime *arn.Anime, malAnime *mal.Anime) []animediff.Difference // Synopsis if len(anime.Summary) < 300 && len(anime.Summary)+50 < len(malAnime.Synopsis) { - hash := utils.HashString(malAnime.Synopsis) + hash := hash.String(malAnime.Synopsis) if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "Synopsis", hash) { differences = append(differences, &animediff.Synopsis{ diff --git a/utils/HashStrings.go b/utils/HashStrings.go deleted file mode 100644 index 69a3e806..00000000 --- a/utils/HashStrings.go +++ /dev/null @@ -1,24 +0,0 @@ -package utils - -import "github.com/OneOfOne/xxhash" - -// HashString returns a hash of the string. -func HashString(item string) uint64 { - h := xxhash.NewS64(0) - h.Write([]byte(item)) - return h.Sum64() -} - -// HashStringsNoOrder returns a hash of the string slice contents, ignoring order. -func HashStringsNoOrder(items []string) uint64 { - sum := uint64(0) - - for _, item := range items { - h := xxhash.NewS64(0) - h.Write([]byte(item)) - numHash := h.Sum64() - sum += numHash - } - - return sum -} diff --git a/utils/HashStringsNoOrder.go b/utils/HashStringsNoOrder.go new file mode 100644 index 00000000..6e87aaaa --- /dev/null +++ b/utils/HashStringsNoOrder.go @@ -0,0 +1,14 @@ +package utils + +import "github.com/akyoto/hash" + +// HashStringsNoOrder returns a hash of the string slice contents, ignoring order. +func HashStringsNoOrder(items []string) uint64 { + sum := uint64(0) + + for _, item := range items { + sum += hash.String(item) + } + + return sum +} diff --git a/utils/editform/editform.go b/utils/editform/editform.go index 5e513d7f..84014cd3 100644 --- a/utils/editform/editform.go +++ b/utils/editform/editform.go @@ -106,7 +106,20 @@ func RenderField(b *strings.Builder, v *reflect.Value, field reflect.StructField // Embedded fields if field.Anonymous { - RenderObject(b, fieldValue.Interface(), idPrefix) + // If it's exported, render it normally. + if field.PkgPath == "" { + RenderObject(b, fieldValue.Interface(), idPrefix) + } + + // If it's an unexported struct, we try to access the field names + // given to us from the anonymous struct on the actual object. + if field.Type.Kind() == reflect.Struct { + for i := 0; i < field.Type.NumField(); i++ { + f := field.Type.Field(i) + RenderField(b, v, f, idPrefix) + } + } + return }