Skip to content

Commit 5e705a1

Browse files
committed
refactor(lint): improve error handling and code consistency across multiple files
1 parent 9540ef6 commit 5e705a1

56 files changed

Lines changed: 194 additions & 214 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.golangci.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@
1717

1818
version: "2"
1919
linters:
20-
default: none
20+
exclusions:
21+
paths:
22+
- answer-data
23+
- ui
24+
- i18n
25+
enable:
26+
- asasalint # checks for pass []any as any in variadic func(...any)
27+
- asciicheck # checks that your code does not contain non-ASCII identifiers
28+
- bidichk # checks for dangerous unicode character sequences
29+
- bodyclose # checks whether HTTP response body is closed successfully
30+
- canonicalheader # checks whether net/http.Header uses canonical header
31+
- copyloopvar # detects places where loop variables are copied (Go 1.22+)
2132

2233
formatters:
2334
enable:

cmd/wire.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build wireinject
2-
// +build wireinject
32

43
/*
54
* Licensed to the Apache Software Foundation (ASF) under one
@@ -32,7 +31,7 @@ import (
3231
"github.com/apache/answer/internal/base/server"
3332
"github.com/apache/answer/internal/base/translator"
3433
"github.com/apache/answer/internal/controller"
35-
"github.com/apache/answer/internal/controller/template_render"
34+
templaterender "github.com/apache/answer/internal/controller/template_render"
3635
"github.com/apache/answer/internal/controller_admin"
3736
"github.com/apache/answer/internal/repo"
3837
"github.com/apache/answer/internal/router"

internal/base/conf/conf.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ func ReadConfig(configFilePath string) (c *AllConfig, err error) {
117117
func RewriteConfig(configFilePath string, allConfig *AllConfig) error {
118118
buf := bytes.Buffer{}
119119
enc := yaml.NewEncoder(&buf)
120-
defer enc.Close()
120+
defer func() {
121+
_ = enc.Close()
122+
}()
121123
enc.SetIndent(2)
122124
if err := enc.Encode(allConfig); err != nil {
123125
return err

internal/base/constant/ctx_flag.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ const (
2323
AcceptLanguageFlag = "Accept-Language"
2424
ShortIDFlag = "Short-ID-Enabled"
2525
)
26+
27+
type ContextKey string
28+
29+
const (
30+
AcceptLanguageContextKey ContextKey = ContextKey(AcceptLanguageFlag)
31+
ShortIDContextKey ContextKey = ContextKey(ShortIDFlag)
32+
)

internal/base/data/data.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Data struct {
4747
func NewData(db *xorm.Engine, cache cache.Cache) (*Data, func(), error) {
4848
cleanup := func() {
4949
log.Info("closing the data resources")
50-
db.Close()
50+
_ = db.Close()
5151
}
5252
return &Data{DB: db, Cache: cache}, cleanup, nil
5353
}

internal/base/handler/lang.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func GetLang(ctx *gin.Context) i18n.Language {
3838

3939
// GetLangByCtx get language from header
4040
func GetLangByCtx(ctx context.Context) i18n.Language {
41-
acceptLanguage, ok := ctx.Value(constant.AcceptLanguageFlag).(i18n.Language)
41+
acceptLanguage, ok := ctx.Value(constant.AcceptLanguageContextKey).(i18n.Language)
4242
if ok {
4343
return acceptLanguage
4444
}

internal/base/handler/short_id.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
// GetEnableShortID get language from header
2929
func GetEnableShortID(ctx context.Context) bool {
30-
flag, ok := ctx.Value(constant.ShortIDFlag).(bool)
30+
flag, ok := ctx.Value(constant.ShortIDContextKey).(bool)
3131
if ok {
3232
return flag
3333
}

internal/base/server/http_funcmap.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package server
2121

2222
import (
2323
"html/template"
24-
"math"
2524
"regexp"
2625
"strconv"
2726
"strings"
@@ -107,15 +106,15 @@ var funcMap = template.FuncMap{
107106
}
108107

109108
if between >= 60 && between < 3600 {
110-
min := math.Floor(float64(between / 60))
109+
min := between / 60
111110
trans = translator.GlobalTrans.Tr(la, "ui.dates.x_minutes_ago")
112-
return strings.ReplaceAll(trans, "{{count}}", strconv.FormatFloat(min, 'f', 0, 64))
111+
return strings.ReplaceAll(trans, "{{count}}", strconv.FormatInt(min, 10))
113112
}
114113

115114
if between >= 3600 && between < 3600*24 {
116-
h := math.Floor(float64(between / 3600))
115+
h := between / 3600
117116
trans = translator.GlobalTrans.Tr(la, "ui.dates.x_hours_ago")
118-
return strings.ReplaceAll(trans, "{{count}}", strconv.FormatFloat(h, 'f', 0, 64))
117+
return strings.ReplaceAll(trans, "{{count}}", strconv.FormatInt(h, 10))
119118
}
120119

121120
if between >= 3600*24 &&

internal/base/validator/validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func Sanitizer(fl validator.FieldLevel) (res bool) {
142142
switch field.Kind() {
143143
case reflect.String:
144144
filter := bluemonday.UGCPolicy()
145-
content := strings.Replace(filter.Sanitize(field.String()), "&amp;", "&", -1)
145+
content := strings.ReplaceAll(filter.Sanitize(field.String()), "&amp;", "&")
146146
field.SetString(content)
147147
return true
148148
case reflect.Chan, reflect.Map, reflect.Slice, reflect.Array:

internal/cli/build.go

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"github.com/Masterminds/semver/v3"
3535
"github.com/apache/answer/pkg/dir"
3636
"github.com/apache/answer/pkg/writer"
37-
"github.com/apache/answer/ui"
3837
"github.com/segmentfault/pacman/log"
3938
"gopkg.in/yaml.v3"
4039
)
@@ -300,50 +299,6 @@ func copyUIFiles(b *buildingMaterial) (err error) {
300299
return nil
301300
}
302301

303-
// overwriteIndexTs overwrites index.ts file in ui/src/plugins/ dir
304-
func overwriteIndexTs(b *buildingMaterial) (err error) {
305-
localUIPluginDir := filepath.Join(b.tmpDir, "vendor/github.com/apache/answer/ui/src/plugins/")
306-
307-
folders, err := getFolders(localUIPluginDir)
308-
if err != nil {
309-
return fmt.Errorf("failed to get folders: %w", err)
310-
}
311-
312-
content := generateIndexTsContent(folders)
313-
err = os.WriteFile(filepath.Join(localUIPluginDir, "index.ts"), []byte(content), 0644)
314-
if err != nil {
315-
return fmt.Errorf("failed to write index.ts: %w", err)
316-
}
317-
return nil
318-
}
319-
320-
func getFolders(dir string) ([]string, error) {
321-
var folders []string
322-
files, err := os.ReadDir(dir)
323-
if err != nil {
324-
return nil, err
325-
}
326-
for _, file := range files {
327-
if file.IsDir() && file.Name() != "builtin" {
328-
folders = append(folders, file.Name())
329-
}
330-
}
331-
return folders, nil
332-
}
333-
334-
func generateIndexTsContent(folders []string) string {
335-
builder := &strings.Builder{}
336-
builder.WriteString("export default null;\n")
337-
// Line 2:1: Delete `⏎` prettier/prettier
338-
if len(folders) > 0 {
339-
builder.WriteString("\n")
340-
}
341-
for _, folder := range folders {
342-
builder.WriteString(fmt.Sprintf("export { default as %s } from '%s';\n", folder, folder))
343-
}
344-
return builder.String()
345-
}
346-
347302
// buildUI run pnpm install and pnpm build commands to build ui
348303
func buildUI(b *buildingMaterial) (err error) {
349304
localUIBuildDir := filepath.Join(b.tmpDir, "vendor/github.com/apache/answer/ui")
@@ -362,13 +317,6 @@ func buildUI(b *buildingMaterial) (err error) {
362317
return nil
363318
}
364319

365-
func replaceNecessaryFile(b *buildingMaterial) (err error) {
366-
fmt.Printf("try to replace ui build directory\n")
367-
uiBuildDir := filepath.Join(b.tmpDir, "vendor/github.com/apache/answer/ui")
368-
err = copyDirEntries(ui.Build, ".", uiBuildDir)
369-
return err
370-
}
371-
372320
// mergeI18nFiles merge i18n files
373321
func mergeI18nFiles(b *buildingMaterial) (err error) {
374322
fmt.Printf("try to merge i18n files\n")

0 commit comments

Comments
 (0)