WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
go: [1.15, 1.16, 1.17, 1.18]
go: [1.23, 1.24, 1.25]
name: ${{ matrix.os }} @ Go ${{ matrix.go }}
runs-on: ${{ matrix.os }}
steps:
- name: Set up Go ${{ matrix.go }}
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}

- name: Cache
uses: actions/cache@v2.1.0
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-

- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v5
with:
path: ${{ env.WORKSPACE }}

Expand All @@ -44,7 +44,7 @@ jobs:
run: sh testdata/test.sh

- name: Upload coverage to Codecov
if: success() && matrix.go == 1.18 && matrix.os == 'windows-latest'
if: success() && matrix.go == 1.25 && matrix.os == 'windows-latest'
uses: codecov/codecov-action@v1
with:
token:
Expand All @@ -56,11 +56,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v5
with:
path: ${{ env.WORKSPACE }}
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v8
with:
version: latest
working-directory: ${{ env.WORKSPACE }}
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "2"

linters:
exclusions:
rules:
- linters: [staticcheck]
text: ST1001
31 changes: 17 additions & 14 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"io"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestGenerateCodeErrorInput(t *testing.T) {
{
d := "./noname"

err := generate(d, iFile, oDir, oFile, ptr, false, true, false, false, ioutil.Discard)
err := generate(d, iFile, oDir, oFile, ptr, false, true, false, false, io.Discard)
if err == nil {
t.Fatal("error has to return")
}
Expand All @@ -91,7 +91,7 @@ func TestGenerateCodeErrorInput(t *testing.T) {
d := "./noname"
f := "foo.go"

err := generate(d, f, oDir, oFile, ptr, false, true, false, false, ioutil.Discard)
err := generate(d, f, oDir, oFile, ptr, false, true, false, false, io.Discard)
if err == nil {
t.Fatal("error has to return")
}
Expand All @@ -102,7 +102,7 @@ func TestGenerateCodeErrorInput(t *testing.T) {
{
d := "main.go"

err := generate(d, iFile, oDir, oFile, ptr, false, true, false, false, ioutil.Discard)
err := generate(d, iFile, oDir, oFile, ptr, false, true, false, false, io.Discard)
if err == nil {
t.Fatal("error has to return")
}
Expand All @@ -113,7 +113,7 @@ func TestGenerateCodeErrorInput(t *testing.T) {
{
f := "foo.go"

err := generate(iDir, f, oDir, oFile, ptr, false, true, false, false, ioutil.Discard)
err := generate(iDir, f, oDir, oFile, ptr, false, true, false, false, io.Discard)
if err == nil {
t.Fatal("error has to return")
}
Expand All @@ -124,7 +124,7 @@ func TestGenerateCodeErrorInput(t *testing.T) {
{
f := "internal"

err := generate(iDir, f, oDir, oFile, ptr, false, true, false, false, ioutil.Discard)
err := generate(iDir, f, oDir, oFile, ptr, false, true, false, false, io.Discard)
if err == nil {
t.Fatal("error has to return")
}
Expand All @@ -135,7 +135,7 @@ func TestGenerateCodeErrorInput(t *testing.T) {
{
f := "./testdata/test.sh"

err := generate(iDir, f, oDir, oFile, ptr, false, true, false, false, ioutil.Discard)
err := generate(iDir, f, oDir, oFile, ptr, false, true, false, false, io.Discard)
if err == nil {
t.Fatal("error has to return")
}
Expand All @@ -146,15 +146,15 @@ func TestGenerateCodeErrorInput(t *testing.T) {
{
d := "./noname"

err := generate(iDir, iFile, d, oFile, ptr, false, true, false, false, ioutil.Discard)
err := generate(iDir, iFile, d, oFile, ptr, false, true, false, false, io.Discard)
if err != nil {
t.Fatal(err)
}
}
{
d := "./main.go"

err := generate(iDir, iFile, d, oFile, ptr, false, true, false, false, ioutil.Discard)
err := generate(iDir, iFile, d, oFile, ptr, false, true, false, false, io.Discard)
if err == nil {
t.Fatal("error has to return")
}
Expand All @@ -173,7 +173,7 @@ func TestGenerateCodeGoPathOutside(t *testing.T) {
t.Fatal(err)
}

err = generate(iDir, iFile, oDir, oFile, ptr, true, true, false, false, ioutil.Discard)
err = generate(iDir, iFile, oDir, oFile, ptr, true, true, false, false, io.Discard)
if err == nil {
t.Fatal("error has to return")
}
Expand All @@ -191,7 +191,7 @@ func TestGenerateCodeDuplicateTag(t *testing.T) {

f := "./testdata/def.go"

err := generate(iDir, f, oDir, oFile, ptr, false, true, false, false, ioutil.Discard)
err := generate(iDir, f, oDir, oFile, ptr, false, true, false, false, io.Discard)
if err == nil {
t.Fatal("error has to return")
}
Expand All @@ -202,7 +202,7 @@ func TestGenerateCodeDuplicateTag(t *testing.T) {

func TestGenerateCodeDryRun(t *testing.T) {

err := generate(iDir, iFile, "", oFile, -1, false, true, false, false, ioutil.Discard)
err := generate(iDir, iFile, "", oFile, -1, false, true, false, false, io.Discard)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -236,8 +236,11 @@ func TestGenerateCodeOK(t *testing.T) {
t.Fatal(err)
}
goPath := strings.SplitN(filepath.ToSlash(wd), "/src", 2)[0]
os.Setenv("GOPATH", filepath.FromSlash(goPath))
err = generate(iDir, iFile, oDir, oFile, ptr, true, true, false, false, ioutil.Discard)
err = os.Setenv("GOPATH", filepath.FromSlash(goPath))
if err != nil {
t.Fatal(err)
}
err = generate(iDir, iFile, oDir, oFile, ptr, true, true, false, false, io.Discard)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/shamaton/msgpackgen

go 1.13
go 1.23

require (
github.com/dave/jennifer v1.4.1
github.com/shamaton/msgpack/v2 v2.1.0
github.com/dave/jennifer v1.7.1
github.com/shamaton/msgpack/v2 v2.3.1
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/dave/jennifer v1.4.1 h1:XyqG6cn5RQsTj3qlWQTKlRGAyrTcsk1kUmWdZBzRjDw=
github.com/dave/jennifer v1.4.1/go.mod h1:7jEdnm+qBcxl8PC0zyp7vxcpSRnzXSt9r39tpTVGlwA=
github.com/shamaton/msgpack/v2 v2.1.0 h1:9jJ2eGZw2Wa9KExPX3KaDDckVjgr4zhXGFCfWagUWqg=
github.com/shamaton/msgpack/v2 v2.1.0/go.mod h1:aTUEmh31ziGX1Ml7wMPLVY0f4vT3CRsCvZRoSCs+VGg=
github.com/dave/jennifer v1.7.1 h1:B4jJJDHelWcDhlRQxWeo0Npa/pYKBLrirAQoTN45txo=
github.com/dave/jennifer v1.7.1/go.mod h1:nXbxhEmQfOZhWml3D1cDK5M1FLnMSozpbFN/m3RmGZc=
github.com/shamaton/msgpack/v2 v2.3.1 h1:R3QNLIGA/tbdczNMZ5PCRxrXvy+fnzsIaHG4kKMgWYo=
github.com/shamaton/msgpack/v2 v2.3.1/go.mod h1:6khjYnkx73f7VQU7wjcFS9DFjs+59naVWJv1TB7qdOI=
4 changes: 2 additions & 2 deletions internal/generator/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -55,7 +55,7 @@ func (g *generator) getImportPathAndParseFile(file string) (string, string, *ast
return "", "", nil, err
}

source, err := ioutil.ReadFile(file)
source, err := os.ReadFile(file)
if err != nil {
return "", "", nil, err
}
Expand Down
9 changes: 4 additions & 5 deletions internal/generator/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"go/ast"
"go/token"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -164,8 +163,8 @@ func (g *generator) run(input, out, fileName string, isInputDir, dryRun bool, w
f := g.generateCode()

if dryRun {
fmt.Fprintf(w, "%#v", f)
return nil
_, err = fmt.Fprintf(w, "%#v", f)
return err
}
err = g.output(f, fileName)
if err != nil {
Expand All @@ -187,7 +186,7 @@ func (g *generator) searchGoModFile(input string, isInputDir bool) (string, erro
return goModFilePath, err
}

files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
return goModFilePath, err
}
Expand Down Expand Up @@ -318,7 +317,7 @@ func (g *generator) getImportPath(path string) (string, error) {
}

func (g *generator) getTargetFiles(dir string, recursive bool) ([]string, error) {
files, err := ioutil.ReadDir(dir)
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
Expand Down
9 changes: 7 additions & 2 deletions internal/generator/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,21 @@ func TestOutput(t *testing.T) {
t.Fatal("something wrong", err)
}

err = os.MkdirAll("tmp/resolver.go", 0777)
if err != nil {
t.Fatal("unexpected error", err)
}

g.outputDir = "tmp"
err = g.output(nil, "")
err = g.output(nil, "resolver.go")
if err == nil {
t.Error("error should occur")
}
if err != nil && !strings.Contains(err.Error(), "is a directory") {
t.Error("something wrong", err)
}

err = os.Remove("tmp")
err = os.RemoveAll("tmp/resolver.go")
if err != nil {
t.Fatal(err)
}
Expand Down
27 changes: 25 additions & 2 deletions internal/generator/structure/code_ident.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package structure

import (
"strings"
"unicode"

. "github.com/dave/jennifer/jen"
"github.com/shamaton/msgpackgen/internal/generator/ptn"
Expand All @@ -12,10 +13,10 @@ type identCodeGen struct {

func (st *Structure) createIdentCode(node *Node, encodeFieldName, decodeFieldName string) (cArray []Code, cMap []Code, eArray []Code, eMap []Code, dArray []Code, dMap []Code) {

funcSuffix := strings.Title(node.IdenticalName)

g := identCodeGen{}

funcSuffix := g.toPascalCase(node.IdenticalName)

cArray = g.createCalcCode("Calc"+funcSuffix, Id(encodeFieldName))
cMap = g.createCalcCode("Calc"+funcSuffix, Id(encodeFieldName))

Expand Down Expand Up @@ -67,3 +68,25 @@ func (g identCodeGen) createDecCode(node *Node, structures []*Structure, fieldNa

return []Code{Block(codes...)}
}

func (g identCodeGen) toPascalCase(s string) string {
parts := strings.FieldsFunc(s, func(r rune) bool {
// character that is not a letter or digit
return !unicode.IsLetter(r) && !unicode.IsDigit(r)
})

if len(parts) == 0 {
return ""
}

result := ""
for _, p := range parts {
if p == "" {
continue
}
// Convert first character to uppercase and the rest to lowercase
result += strings.ToUpper(string(p[0])) + strings.ToLower(p[1:])
}

return result
}
1 change: 1 addition & 0 deletions msgpack/enc/byte.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (e *Encoder) CalcByte(b byte) int {
}

// WriteByte sets the contents of v to the buffer.
//nolint:govet // custom WriteByte signature is intentional (used only by generated code)
func (e *Encoder) WriteByte(b byte, offset int) int {
return e.setByte(b, offset)
}
2 changes: 1 addition & 1 deletion msgpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ func TestStruct(t *testing.T) {
if !reflect.DeepEqual(v.A, v1.A) || !reflect.DeepEqual(v.A, v2.A) {
t.Error("value different", v.A, v1.A, v2.A)
}
if v.A.B.Int != v1.B.Int || v.B.Int != v2.A.B.Int {
if v.A.B.Int != v1.B.Int || v.B.Int != v2.A.B.Int { //nolint:staticcheck // keeping "A" explicit for clarity
t.Error("value something wrong", v.A.Int, v1.Int, v2.Int)
}
if v.A.Int == v1.Int || v.A.Int == v2.Int {
Expand Down
Loading