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
3 changes: 3 additions & 0 deletions src/compress/golang/plugin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.0
require (
github.com/davecgh/go-spew v1.1.1
golang.org/x/tools v0.30.0
github.com/cloudwego/abcoder/src/uniast v0.0.0
)

require (
Expand All @@ -18,3 +19,5 @@ require (
golang.org/x/sync v0.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/cloudwego/abcoder/src/uniast => ../../../uniast
89 changes: 47 additions & 42 deletions src/compress/golang/plugin/parse/ctx.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2025 CloudWeGo Authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -24,6 +24,8 @@ import (
"path/filepath"
"strings"

"github.com/cloudwego/abcoder/src/uniast"
. "github.com/cloudwego/abcoder/src/uniast"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个副作用 import 的目的是啥

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

引入 pub struct 吗?带上前缀的方式可能会清晰一些?

Copy link
Collaborator Author

@AsterDY AsterDY Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

就不用到处引用都得加上uniast.了。。。改动有点大

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

Expand Down Expand Up @@ -54,7 +56,13 @@ func isExternalID(id *Identity, curmod string) bool {
strings.Contains(id.PkgPath, "/kitex_gen/") || strings.Contains(id.PkgPath, "/hertz_gen/")
}

func (p *goParser) referCodes(ctx *fileContext, id *Identity, depth int) (err error) {
func newModule(mod string, dir string) *Module {
ret := uniast.NewModule(mod, dir)
ret.Language = Golang
return ret
}

func (p *GoParser) referCodes(ctx *fileContext, id *Identity, depth int) (err error) {
if depth == 0 || id.PkgPath == "" || !isExternalID(id, ctx.module.Name) {
return nil
}
Expand All @@ -70,7 +78,8 @@ func (p *goParser) referCodes(ctx *fileContext, id *Identity, depth int) (err er
// }()
mod := p.repo.Modules[id.ModPath]
if mod == nil {
mod = NewModule(id.ModPath, "")
mod = newModule(id.ModPath, "")
mod.Language = uniast.Golang
p.repo.Modules[id.ModPath] = mod
}
// fmt.Printf("refer code for %v\n", id.Full())
Expand All @@ -96,7 +105,7 @@ func (p *goParser) referCodes(ctx *fileContext, id *Identity, depth int) (err er
return
}

func (p *goParser) getFileBytes(path string) []byte {
func (p *GoParser) getFileBytes(path string) []byte {
if bs, ok := p.files[path]; ok {
return bs
}
Expand Down Expand Up @@ -160,28 +169,27 @@ func (ctx *fileContext) GetRawContent(node ast.Node) []byte {

func GetRawContent(fset *token.FileSet, file []byte, node ast.Node) []byte {
var doc = bytes.Buffer{}
if collectComment {
switch v := node.(type) {
case *ast.GenDecl:
if v.Doc != nil {
doc.Write(file[fset.Position(v.Doc.Pos()).Offset:fset.Position(v.Doc.End()).Offset])
doc.WriteByte('\n')
}
case *ast.TypeSpec:
if v.Doc != nil {
doc.Write(file[fset.Position(v.Doc.Pos()).Offset:fset.Position(v.Doc.End()).Offset])
doc.WriteByte('\n')
}
case *ast.ValueSpec:
if v.Doc != nil {
doc.Write(file[fset.Position(v.Doc.Pos()).Offset:fset.Position(v.Doc.End()).Offset])
doc.WriteByte('\n')
}
case *ast.FuncDecl:
if v.Doc != nil {
doc.Write(file[fset.Position(v.Doc.Pos()).Offset:fset.Position(v.Doc.End()).Offset])
doc.WriteByte('\n')
}
switch v := node.(type) {
case *ast.GenDecl:
if collectComment && v.Doc != nil {
doc.Write(file[fset.Position(v.Doc.Pos()).Offset:fset.Position(v.Doc.End()).Offset])
doc.WriteByte('\n')
}
case *ast.TypeSpec:
if collectComment && v.Doc != nil {
doc.Write(file[fset.Position(v.Doc.Pos()).Offset:fset.Position(v.Doc.End()).Offset])
doc.WriteByte('\n')
}
doc.WriteString("type ")
case *ast.ValueSpec:
if collectComment && v.Doc != nil {
doc.Write(file[fset.Position(v.Doc.Pos()).Offset:fset.Position(v.Doc.End()).Offset])
doc.WriteByte('\n')
}
case *ast.FuncDecl:
if collectComment && v.Doc != nil {
doc.Write(file[fset.Position(v.Doc.Pos()).Offset:fset.Position(v.Doc.End()).Offset])
doc.WriteByte('\n')
}
}
doc.Write(file[fset.Position(node.Pos()).Offset:fset.Position(node.End()).Offset])
Expand All @@ -201,14 +209,18 @@ func (ctx *fileContext) GetTypeId(typ ast.Expr) (x Identity, isPointer bool, isS
}
}

func (ctx *fileContext) collectFields(fields []*ast.Field, m *[]Identity) {
func (ctx *fileContext) collectFields(fields []*ast.Field, m *[]Dependency) {
for _, fieldDecl := range fields {
id, _, isStdOrBuiltin := ctx.GetTypeId(fieldDecl.Type)
if isStdOrBuiltin || id.PkgPath == "" {
continue
}
*m = append(*m, id)
*m = append(*m, Dependency{
Identity: id,
FileLine: ctx.FileLine(fieldDecl),
})
}
return
}

type importInfo struct {
Expand All @@ -218,7 +230,7 @@ type importInfo struct {
Origins []string
}

func (p *goParser) mockTypes(typ ast.Expr, m map[string]Identity, file []byte, fset *token.FileSet, fpath string, mod string, pkg string, impts *importInfo) (name string, isPointer bool) {
func (p *GoParser) mockTypes(typ ast.Expr, m map[string]Identity, file []byte, fset *token.FileSet, fpath string, mod string, pkg string, impts *importInfo) (name string, isPointer bool) {
ids, _, isP := getTypeName(fset, file, typ)
for _, id := range ids {
// NOTICE: mock all types in the module
Expand Down Expand Up @@ -319,18 +331,19 @@ func getTypeName(fset *token.FileSet, file []byte, typ ast.Expr) (ret []Identity
return
}

func (p *goParser) collectTypes(ctx *fileContext, typ ast.Expr, st *Type, inlined bool) {
func (p *GoParser) collectTypes(ctx *fileContext, typ ast.Expr, st *Type, inlined bool) {
id, _, isGoBuiltins := ctx.GetTypeId(typ)
dep := NewDependency(id, ctx.FileLine(typ))
if isGoBuiltins || id.PkgPath == "" {
return
}
if err := p.referCodes(ctx, &id, p.opts.ReferCodeDepth); err != nil {
fmt.Fprintf(os.Stderr, "failed to get refer code for %s: %v\n", id.Name, err)
}
if inlined {
st.InlineStruct = append(st.InlineStruct, id)
st.InlineStruct = append(st.InlineStruct, dep)
} else {
st.SubStruct = append(st.SubStruct, id)
st.SubStruct = append(st.SubStruct, dep)
}
}

Expand Down Expand Up @@ -393,11 +406,3 @@ func (ctx *fileContext) IsSysImport(alias string) bool {
_, ok := ctx.imports.SysImports[alias]
return ok
}

// FileLine represents a filename and line number
type FileLine struct {
File string

// NOTICE: line number start from 1
Line int
}
Loading