Fix golint errors and gofmt

This commit is contained in:
2021-12-08 13:18:14 -08:00
parent c8bec472be
commit 254155d442
10 changed files with 144 additions and 21 deletions

View File

@@ -44,6 +44,8 @@ func NewCalcCard(query, _ string) Card {
return &CalcCard{query: query, useFunc: true}
}
// Matches determines whether a query matches the requirements
// fot CalcCard and determines which function to run with what arguments
func (cc *CalcCard) Matches() bool {
// If query has solve prefix
if strings.HasPrefix(cc.query, "solve") {
@@ -106,6 +108,7 @@ func (cc *CalcCard) Matches() bool {
return false
}
// StripKey removes all key words related to CalcCard
func (cc *CalcCard) StripKey() string {
// Compile regex for words to be removed
trimRgx := regexp.MustCompile(`^(.*?)solve|integrate|integral(?: of)?|diff|differentiate|derivative(?: of)?|derive|calculate(.*)$`)
@@ -113,6 +116,7 @@ func (cc *CalcCard) StripKey() string {
return trimRgx.ReplaceAllString(cc.query, "${1}${2}")
}
// Content returns the solveRenderScript with the given input
func (cc *CalcCard) Content() template.HTML {
var input string
// If function is being used
@@ -130,22 +134,29 @@ func (cc *CalcCard) Content() template.HTML {
))
}
// Footer returns empty string because CalcCard has no footer
func (cc *CalcCard) Footer() template.HTML {
return ""
}
// Returned always returns true since CalcCard is a frontend
// card and it is thus impossible to determine whether
// the query gets an answer
func (cc *CalcCard) Returned() bool {
return true
}
// RunQuery returns nil as CalcCard is a frontend card
func (cc *CalcCard) RunQuery() error {
return nil
}
// Title for CalcCard is "Calculation"
func (cc *CalcCard) Title() string {
return "Calculation"
}
// Head returns the extra head tags required for CalcCard
func (cc *CalcCard) Head() template.HTML {
return calcExtraHead
}

View File

@@ -1,13 +1,10 @@
package cards
import (
"errors"
"html/template"
"sort"
)
var ErrCardRegistered = errors.New("card with that name has already been registered")
// cardRegistration represents a card that has been registered
type cardRegistration struct {
name string
@@ -79,7 +76,7 @@ func GetCard(query, userAgent string) Card {
for _, cardReg := range cards {
// Create new card
card := cardReg.newFn(query, userAgent)
// If card matches, return it
// If card matches, return it
if card.Matches() {
return card
}

View File

@@ -39,7 +39,7 @@ type DDGInstAns struct {
AnswerType string
}
// NewDDGCard isa NewCardFunc that creates a new DDGCard
// NewDDGCard is a NewCardFunc that creates a new DDGCard
func NewDDGCard(query, userAgent string) Card {
return &DDGCard{
query: url.QueryEscape(query),
@@ -68,7 +68,7 @@ func (ddg *DDGCard) RunQuery() error {
return err
}
// Decode response into repsonse struct
// Decode response into response struct
err = json.NewDecoder(res.Body).Decode(&ddg.resp)
if err != nil {
return err
@@ -77,30 +77,38 @@ func (ddg *DDGCard) RunQuery() error {
return nil
}
// Returned checks if abstract is empty.
// If it is, query returned no result.
func (ddg *DDGCard) Returned() bool {
// Value was returned if abstract is not empty
return ddg.resp.Abstract != ""
}
// Matches always returns true as there are no keys
func (ddg *DDGCard) Matches() bool {
// Everything matches since there are no keys
return true
}
// StripKey returns the query as there are no keys
func (ddg *DDGCard) StripKey() string {
// No key to strip, so return query
return ddg.query
}
// Title returns the instant answer heading
func (ddg *DDGCard) Title() string {
return ddg.resp.Heading
}
// Content returns the instant answer abstract with
// DuckDuckGo attibution
func (ddg *DDGCard) Content() template.HTML {
// Return abstract with attribution
return template.HTML(ddg.resp.Abstract + fmt.Sprintf(ddgAttribution, ddg.query))
}
// Footer returns a footer containing URL and name of source
// gotten from instant answer API
func (ddg *DDGCard) Footer() template.HTML {
// Return footer with abstract url and source
return template.HTML(fmt.Sprintf(
@@ -110,6 +118,8 @@ func (ddg *DDGCard) Footer() template.HTML {
))
}
// Head returns an empty string as no extra tags are reuired
// for DDGCard
func (ddg *DDGCard) Head() template.HTML {
return ""
}

View File

@@ -166,16 +166,21 @@ func (mc *MetaweatherCard) RunQuery() error {
return nil
}
// Returned checks whether no location was found or response
// said not found.
func (mc *MetaweatherCard) Returned() bool {
return len(mc.location) > 0 && mc.resp.Detail != "Not found."
}
// Matches determines whether the query matches the keys for
// MetaweatherCard
func (mc *MetaweatherCard) Matches() bool {
return strings.HasPrefix(mc.query, "weather in") ||
strings.HasPrefix(mc.query, "weather") ||
strings.HasSuffix(mc.query, "weather")
}
// StripKey removes keys related to MetaweatherCard
func (mc *MetaweatherCard) StripKey() string {
query := strings.TrimPrefix(mc.query, "weather in")
query = strings.TrimPrefix(query, "weather")
@@ -183,6 +188,7 @@ func (mc *MetaweatherCard) StripKey() string {
return strings.TrimSpace(query)
}
// Content returns metaweatherContent with all information added
func (mc *MetaweatherCard) Content() template.HTML {
return template.HTML(fmt.Sprintf(
metaweatherContent,
@@ -213,15 +219,18 @@ func (mc *MetaweatherCard) Content() template.HTML {
))
}
// Title of MetaweatherCard is "Weather"
func (mc *MetaweatherCard) Title() string {
return "Weather"
}
// Footer returns a footer with a link to metaweather
func (mc *MetaweatherCard) Footer() template.HTML {
// Return footer with link to metaweather
return `<div class="card-footer"><a class="card-footer-item" href="https://www.metaweather.com/">Metaweather</a></div>`
}
// Head returns an empty string as no extra head tags
// are required for MetaweatherCard
func (mc *MetaweatherCard) Head() template.HTML {
return ""
}

View File

@@ -43,12 +43,14 @@ func NewPlotCard(query, _ string) Card {
return &PlotCard{query: query}
}
// Matches checks if the query matches the rules for PlotCard
func (pc *PlotCard) Matches() bool {
return strings.HasPrefix(pc.query, "plot") ||
strings.HasPrefix(pc.query, "graph") ||
strings.HasPrefix(pc.query, "draw")
}
// StripKey removes all keys related to PlotCard
func (pc *PlotCard) StripKey() string {
query := strings.TrimPrefix(pc.query, "plot")
query = strings.TrimPrefix(query, "graph")
@@ -56,6 +58,7 @@ func (pc *PlotCard) StripKey() string {
return strings.TrimSpace(query)
}
// Content returns plot script with given input
func (pc *PlotCard) Content() template.HTML {
return template.HTML(fmt.Sprintf(
plotScript,
@@ -63,27 +66,28 @@ func (pc *PlotCard) Content() template.HTML {
))
}
// Since this card is frontend, this cannot be checked.
// Therefore, it will always return true.
// Returned will alwats return true because
// this card is frontend, and this cannot be checked.
func (pc *PlotCard) Returned() bool {
return true
}
// Title generates a title formatted as "Plot (<eqation>)"
func (pc *PlotCard) Title() string {
return "Plot (" + pc.StripKey() + ")"
}
// Head returns extra head tags for PlotCard
func (pc *PlotCard) Head() template.HTML {
return plotExtraHead
}
// Footer returns an empty string as PlotCard has no footer
func (pc *PlotCard) Footer() template.HTML {
return ""
}
// RunQuery returns nil as PlotCard is a frontend card
func (pc *PlotCard) RunQuery() error {
return nil
}