Skip to content

Commit

Permalink
tests: add cgo case
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Dec 22, 2024
1 parent 36263ea commit efd5eb3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
18 changes: 17 additions & 1 deletion tagalign.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"cmp"
"fmt"
"go/ast"
"go/token"
"reflect"
"slices"
"strconv"
"strings"

"github.com/fatih/structtag"

"golang.org/x/tools/go/analysis"
)

Expand Down Expand Up @@ -38,6 +38,13 @@ func NewAnalyzer(options ...Option) *analysis.Analyzer {

func Run(pass *analysis.Pass, options ...Option) {
for _, f := range pass.Files {
filename := getFilename(pass.Fset, f)
if !strings.HasSuffix(filename, ".go") {
continue
}

println(filename)

h := &Helper{
style: DefaultStyle,
align: true,
Expand Down Expand Up @@ -378,3 +385,12 @@ func removeField(fields []*ast.Field, index int) []*ast.Field {

return append(fields[:index], fields[index+1:]...)
}

func getFilename(fset *token.FileSet, file *ast.File) string {
filename := fset.PositionFor(file.Pos(), true).Filename
if !strings.HasSuffix(filename, ".go") {
return fset.PositionFor(file.Pos(), false).Filename
}

return filename
}
6 changes: 6 additions & 0 deletions tagalign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func TestAnalyzer(t *testing.T) {
}
}

func TestAnalyzer_cgo(t *testing.T) {
a := NewAnalyzer()

analysistest.Run(t, analysistest.TestData(), a, "cgo")
}

func Test_alignFormat(t *testing.T) {
format := alignFormat(20)
assert.Equal(t, "%-20s", format)
Expand Down
43 changes: 43 additions & 0 deletions testdata/src/cgo/cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cgo

/*
#include <stdio.h>
#include <stdlib.h>
void myprint(char* s) {
printf("%d\n", s);
}
*/
import "C"

import (
"unsafe"
)

func _() {
cs := C.CString("Hello from stdio\n")
C.myprint(cs)
C.free(unsafe.Pointer(cs))
}

type FooBar struct {
Foo int `json:"foo" validate:"required"` // want `tag is not aligned, should be: json:"foo"`
Bar string `json:"___bar___,omitempty" validate:"required"` // want `json:"___bar___,omitempty" validate:"required"`
FooFoo int8 `json:"foo_foo" validate:"required" yaml:"fooFoo"` // want `tag is not aligned, should be: json:"foo_foo"`
BarBar int `json:"bar_bar" validate:"required"` // want `tag is not aligned, should be: json:"bar_bar"`
FooBar struct {
Foo int `json:"foo" yaml:"foo" validate:"required"` // want `tag is not aligned, should be: json:"foo" yaml:"foo" validate:"required"`
Bar222 string `json:"bar222" validate:"required" yaml:"bar"` // want `tag is not aligned, should be: json:"bar222" validate:"required" yaml:"bar"`
} `json:"foo_bar" validate:"required"`
FooFooFoo struct {
BarBarBar struct {
BarBarBarBar string `json:"bar_bar_bar_bar" validate:"required"` // want `json:"bar_bar_bar_bar" validate:"required"`
BarBarBarFooBar string `json:"bar_bar_bar_foo_bar" yaml:"bar" validate:"required"` // want `tag is not aligned, should be: json:"bar_bar_bar_foo_bar" yaml:"bar" validate:"required"`
} `json:"bar_bar_bar" validate:"required"`
}
BarFooBarFoo struct{}
// test comment
// test commnet 2
BarFoo string `json:"bar_foo" validate:"required"` // want `tag is not aligned, should be: json:"bar_foo" validate:"required"`
BarFooBar string `json:"bar_foo_bar" validate:"required"`
}

0 comments on commit efd5eb3

Please sign in to comment.