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
123 changes: 55 additions & 68 deletions .github/.golangci.yml
Original file line number Diff line number Diff line change
@@ -1,82 +1,69 @@
linters-settings:
dupl:
threshold: 100
errcheck:
check-type-assertions: true
ignore: fmt:.*,io/ioutil:^Read.*,io:Close,os:Close
goconst:
min-len: 2
min-occurrences: 2
goimports:
local-prefixes: github.com/golangci/golangci-lint
golint:
min-confidence: 0
govet:
check-shadowing: true

lll:
line-length: 120
misspell:
locale: US
ignore-words: []

version: "2"
run:
go: "1.25"
linters:
disable-all: true
default: none
enable:
# HTTPリクエストで閉じられていないものを検出
- bodyclose
# _, _ := x() のようなものを検出
- dogsled
# 同一コードの検出
- dupl
# エラーを未チェックのものを検出
- errcheck
# 定数化できるものを検出
- goconst
# gofmt
- gofmt
# goimports
- goimports
# 引数がフォーマット文字列と一致しないものを検出
- govet
# 意味のない再代入を検出
- ineffassign
# 1行の最大長が長すぎるものを検出
- lll
# スペルチェック
- misspell
# linter
- revive
# 不要な型変換を検出
- unconvert
# 未使用のものを検出
- unused
# 前後の余計な空白を検出
- whitespace

issues:
exclude:
- declaration of "(err|ctx)" shadows declaration at
exclude-use-default: false
exclude-rules:
- path: _test\.go
linters:
- gomnd
- dupl

- linters:
- lll
source: "//go:generate "

run:
skip-dirs:
- example
- test/testdata_etc
- internal/cache
- internal/renameio
- internal/robustio

service:
golangci-lint-version: 1.23.x
prepare:
- echo "here I can run custom commands, but no preparation needed for this repo"
settings:
dupl:
threshold: 100
errcheck:
check-type-assertions: true
exclude-functions:
- fmt.*
- io/ioutil.Read.*
- io.Close
- os.Close
- io/fs.Close
goconst:
min-len: 2
min-occurrences: 2
govet:
enable:
- shadow
misspell:
locale: US
exclusions:
generated: lax
rules:
- linters:
- dupl
- mnd
path: _test\.go
- linters:
- lll
source: '//go:generate '
- path: (.+)\.go$
text: declaration of "(err|ctx)" shadows declaration at
paths:
- testdata
- third_party$
- builtin$
- examples$
formatters:
enable:
- gofmt
- goimports
settings:
goimports:
local-prefixes:
- github.com/go-generalize/go-easyparser
exclusions:
generated: lax
paths:
- testdata
- third_party$
- builtin$
- examples$
4 changes: 2 additions & 2 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Go
on:
push:
pull_request:
branches: [ master ]
branches: [ main ]

jobs:

Expand All @@ -15,7 +15,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v5
with:
go-version: "1.22"
go-version: "1.25"
id: go

- name: Check out code into the Go module directory
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: linter
on: [pull_request]
jobs:
golangci-lint:
name: runner / golangci-lint
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
- uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0
with:
go-version: '1.25'
- name: golangci-lint
uses: golangci/golangci-lint-action@9fae48acfc02a90574d7c304a1758ef9895495fa # v7.0.1
with:
args: "--config=.github/.golangci.yml"
version: v2.12.1
only-new-issues: true
skip-cache: true
15 changes: 0 additions & 15 deletions .github/workflows/reviewdog.yaml

This file was deleted.

15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
# go-easyparser
Parse Go packages with ease

## Behavior notes (v0.5.0+)

### Newly supported types
- `*types.Alias` (Go 1.23+ `gotypesalias=1`) is resolved via `types.Unalias`. The alias name itself is not preserved (e.g. `type UserID = string` is parsed as `string`).
- `*types.Signature` (function types) falls back to `Any{}`.
- `*types.TypeParam` (generic type parameters) falls back to `Any{}`.
- Generic type aliases (Go 1.24+, e.g. `type Vec[T any] = []T`) are also handled via `types.Unalias`.

### Migration from previous versions
- Inputs containing `func(...)` fields, generic type parameters, or type aliases used to **panic** with `unsupported named type: *types.<Kind>`. They now parse successfully and produce `Any{}` (or the resolved underlying type for aliases). If your downstream code relied on this panic for fail-fast validation, install a custom `Replacer` that explicitly rejects these kinds.
- The panic message wording changed from `unsupported named type:` to `unsupported type:` (the previous wording was misleading since `Map`, `Slice`, etc. are not named types).

### Replacer caveat
- When a `*types.Alias` is encountered, the user-supplied `Replacer` is invoked twice — once on the original alias type, then again on the Unalias-resolved type via the recursive `parseType` call. `Replacer` implementations should be idempotent.
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
module github.com/go-generalize/go-easyparser

go 1.22
go 1.25.0

require (
github.com/go-utils/gopackages v0.1.0
github.com/google/go-cmp v0.6.0
golang.org/x/tools v0.23.0
golang.org/x/tools v0.44.0
)

require (
golang.org/x/mod v0.19.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/mod v0.35.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9 // indirect
)
62 changes: 6 additions & 56 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,24 @@ github.com/go-utils/gopackages v0.1.0 h1:zXn3KS4HzPGjpUcMZd2LzZ2wQ8K6haxGEebxCyx
github.com/go-utils/gopackages v0.1.0/go.mod h1:QPVW4BzGDze3cEu7TbM7Gw9ppfYTj4NDgBW0JwNZE1g=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.35.0 h1:Ww1D637e6Pg+Zb2KrWfHQUnH2dQRLBQyAtpr/haaJeM=
golang.org/x/mod v0.35.0/go.mod h1:+GwiRhIInF8wPm+4AoT6L0FA1QWAad3OMdTRx4tFYlU=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.15.0 h1:zdAyfUGbYmuVokhzVmghFl2ZJh5QhcfebBgmVPFYA+8=
golang.org/x/tools v0.15.0/go.mod h1:hpksKq4dtpQWS1uQ61JkdqWM3LscIS6Slf+VVkm+wQk=
golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg=
golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI=
golang.org/x/tools v0.44.0 h1:UP4ajHPIcuMjT1GqzDWRlalUEoY+uzoZKnhOjbIPD2c=
golang.org/x/tools v0.44.0/go.mod h1:KA0AfVErSdxRZIsOVipbv3rQhVXTnlU6UhKxHd1seDI=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9 h1:LLhsEBxRTBLuKlQxFBYUOU8xyFgXv6cOTp2HASDlsDk=
golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
12 changes: 11 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,18 @@ func (p *pkgParser) parseType(u types.Type, dep bool) tstypes.Type {
typ = p.parseMap(u)
case *types.Interface:
typ = p.parseInterface(u)
case *types.Alias:
ua := types.Unalias(u)
if ua == nil {
panic("incomplete alias: " + u.String())
}
typ = p.parseType(ua, dep)
case *types.Signature:
typ = &tstypes.Any{}
case *types.TypeParam:
typ = &tstypes.Any{}
default:
panic("unsupported named type: " + reflect.TypeOf(u).String())
panic("unsupported type: " + reflect.TypeOf(u).String())
}

return typ
Expand Down
10 changes: 10 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"

alias "github.com/go-generalize/go-easyparser/testdata/alias"
"github.com/go-generalize/go-easyparser/testdata/conflict"
"github.com/go-generalize/go-easyparser/testdata/recursive"
"github.com/go-generalize/go-easyparser/testdata/replace"
Expand Down Expand Up @@ -119,6 +120,15 @@ func TestParser_Parse(t *testing.T) {
wantRes: replace.Type,
wantErr: false,
},
{
name: "alias",
fields: fields{
pkgs: parse(t, "./testdata/alias/base"),
Filter: All,
},
wantRes: alias.Type,
wantErr: false,
},
}

wd, err := os.Getwd()
Expand Down
37 changes: 37 additions & 0 deletions testdata/alias/base/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package base

// WithMapAny は map value として any(= *types.Alias)を持つ
type WithMapAny struct {
Extra map[string]any `json:"extra"`
}

// WithSliceAny は slice element として any を持つ
type WithSliceAny struct {
Items []any `json:"items"`
}

// UserID は named-basic alias(alias 名喪失挙動の固定)
type UserID = string

// WithUserID は named-basic alias 経由のフィールドを持つ
type WithUserID struct {
ID UserID `json:"id"`
}

// WithHandler は *types.Signature を踏む(Any fallback 検証)
type WithHandler struct {
H func(int) error `json:"handler"`
}

// Box は *types.TypeParam を踏む generic struct
type Box[T any] struct {
V T `json:"v"`
}

// VecAny は Go 1.24+ generic alias(type alias with type parameters)
type VecAny[T any] = []T

// WithVecAny は generic alias を field に持つ
type WithVecAny struct {
Items VecAny[int] `json:"items"`
}
Loading
Loading