Files
gf/encoding/gini/gini.go
houseme 94cc233325 fix: disable specific staticcheck rules and update lint config (#4396)
fix: disable specific staticcheck rules and update lint config

- Disabled staticcheck rules SA1029, SA1019, S1000, and related checks
in `.golangci.yml` to filter out unwanted linter errors.
- Updated staticcheck checks list for more precise linting control.
- Clarified configuration for easier maintenance and future updates.

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: hailaz <739476267@qq.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-08-29 10:32:30 +08:00

126 lines
3.0 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package gini provides accessing and converting for INI content.
package gini
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/json"
)
// Decode converts INI format to map.
func Decode(data []byte) (res map[string]any, err error) {
res = make(map[string]any)
var (
fieldMap = make(map[string]any)
bytesReader = bytes.NewReader(data)
bufioReader = bufio.NewReader(bytesReader)
section string
lastSection string
haveSection bool
line string
)
for {
line, err = bufioReader.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
err = gerror.Wrapf(err, `bufioReader.ReadString failed`)
return nil, err
}
if line = strings.TrimSpace(line); len(line) == 0 {
continue
}
if line[0] == ';' || line[0] == '#' {
continue
}
var (
sectionBeginPos = strings.Index(line, "[")
sectionEndPos = strings.Index(line, "]")
)
if sectionBeginPos >= 0 && sectionEndPos >= 2 {
section = line[sectionBeginPos+1 : sectionEndPos]
if lastSection == "" {
lastSection = section
} else if lastSection != section {
lastSection = section
fieldMap = make(map[string]any)
}
haveSection = true
} else if !haveSection {
continue
}
if strings.Contains(line, "=") && haveSection {
values := strings.Split(line, "=")
fieldMap[strings.TrimSpace(values[0])] = strings.TrimSpace(strings.Join(values[1:], "="))
res[section] = fieldMap
}
}
if !haveSection {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "failed to parse INI file, section not found")
}
return res, nil
}
// Encode converts map to INI format.
func Encode(data map[string]any) (res []byte, err error) {
var (
n int
w = new(bytes.Buffer)
m map[string]any
ok bool
)
for section, item := range data {
// Section key-value pairs.
if m, ok = item.(map[string]any); ok {
n, err = fmt.Fprintf(w, "[%s]\n", section)
if err != nil || n == 0 {
return nil, gerror.Wrapf(err, "w.WriteString failed")
}
for k, v := range m {
if n, err = fmt.Fprintf(w, "%s=%v\n", k, v); err != nil || n == 0 {
return nil, gerror.Wrapf(err, "w.WriteString failed")
}
}
continue
}
// Simple key-value pairs.
for k, v := range data {
if n, err = fmt.Fprintf(w, "%s=%v\n", k, v); err != nil || n == 0 {
return nil, gerror.Wrapf(err, "w.WriteString failed")
}
}
break
}
res = make([]byte, w.Len())
if n, err = w.Read(res); err != nil || n == 0 {
return nil, gerror.Wrapf(err, "w.Read failed")
}
return res, nil
}
// ToJson convert INI format to JSON.
func ToJson(data []byte) (res []byte, err error) {
iniMap, err := Decode(data)
if err != nil {
return nil, err
}
return json.Marshal(iniMap)
}