Skip to content

Commit

Permalink
fix: allow well-known headers
Browse files Browse the repository at this point in the history
See: #12
  • Loading branch information
lasiar committed May 9, 2024
1 parent 7da5457 commit 585255d
Show file tree
Hide file tree
Showing 7 changed files with 592 additions and 2 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/diff.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: check diff

on:
push:
tags:
- '*'

permissions:
contents: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: stable
check-latest: true

- name: Generate test
run: |
make generate
- name: Check diff
run: |
git diff --exit-code
17 changes: 15 additions & 2 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func run(pass *analysis.Pass) (any, error) {
return nil, fmt.Errorf("want %T, got %T", spctor, pass.ResultOf[inspect.Analyzer])
}

wellKnownHeaders := initialism()

nodeFilter := []ast.Node{
(*ast.CallExpr)(nil),
}
Expand Down Expand Up @@ -97,8 +99,8 @@ func run(pass *analysis.Pass) (any, error) {
return
}

headerKeyCanonical := http.CanonicalHeaderKey(headerKeyOriginal)
if headerKeyOriginal == headerKeyCanonical {
headerKeyCanonical, isWellKnown := canonicalHeaderKey(headerKeyOriginal, wellKnownHeaders)
if headerKeyOriginal == headerKeyCanonical || isWellKnown {
return
}

Expand Down Expand Up @@ -131,6 +133,17 @@ func run(pass *analysis.Pass) (any, error) {
return nil, outerErr
}

func canonicalHeaderKey(s string, m map[string]string) (string, bool) {
canonical := http.CanonicalHeaderKey(s)

wellKnown, ok := m[canonical]
if !ok {
return canonical, ok
}

return wellKnown, ok
}

func isHTTPHeader(named *types.Named) bool {
return named.Obj() != nil &&
named.Obj().Pkg() != nil &&
Expand Down
1 change: 1 addition & 0 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestAnalyzer(t *testing.T) {
"common",
"embedded",
"global",
"initialism",
"struct",
}

Expand Down
Loading

0 comments on commit 585255d

Please sign in to comment.