Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 4 additions & 11 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,7 @@ import (
"path/filepath"
"strings"

. "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 @@ -393,11 +394,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
}
98 changes: 5 additions & 93 deletions src/compress/golang/plugin/parse/file.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// 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.
// See the License for the specific language governing permissions and
// limitations under the License.


package parse

import (
Expand All @@ -23,45 +22,9 @@ import (
"os"
"strconv"
"strings"
)

// Function holds the information about a function
type Function struct {
Exported bool

IsMethod bool // If the function is a method
Identity // unique identity in a repo
FileLine
Content string // Content of the function, including functiion signature and body

Receiver *Receiver `json:",omitempty"` // Method receiver
Params []Identity `json:",omitempty"` // function parameters, key is the parameter name
Results []Identity `json:",omitempty"` // function results, key is the result name or type name

// call to in-the-project functions, key is {{pkgAlias.funcName}} or {{funcName}}
FunctionCalls []Identity `json:",omitempty"`

// call to internal methods,
// NOTICE: method name may be duplicated, so we collect according to the SEQUENCE of APPEARANCE
MethodCalls []Identity `json:",omitempty"`

Types []Identity `json:",omitempty"` // types used in the function
GolobalVars []Identity `json:",omitempty"` // global vars used in the function

// func llm compress result
CompressData *string `json:"compress_data,omitempty"`
}

type Receiver struct {
IsPointer bool
Type Identity
Name string
}

type Dependency struct {
Range [2]int32
Identity
}
. "github.com/cloudwego/abcoder/src/uniast"
)

func (p *goParser) parseFile(ctx *fileContext, f *ast.File) error {
cont := true
Expand Down Expand Up @@ -115,17 +78,6 @@ func (p *goParser) parseFile(ctx *fileContext, f *ast.File) error {
return nil
}

type Var struct {
IsExported bool
IsConst bool
Identity
FileLine
Type *Identity `json:",omitempty"`
Content string

CompressData *string `json:"compress_data,omitempty"`
}

func (p *goParser) newVar(mod string, pkg string, name string, isConst bool) *Var {
ret := &Var{
Identity: NewIdentity(mod, pkg, name),
Expand Down Expand Up @@ -460,46 +412,6 @@ set_func:
return f, false
}

type TypeKind int

const (
TypeKindStruct = 0 // type struct
TypeKindInterface = 1 // type interface
TypeKindNamed = 2 // type NamedXXX other..
TypeKindEnum = 3 // type NamedXXX other..
)

// Type holds the information about a struct
type Type struct {
Exported bool // if the struct is exported

TypeKind // type Kind: Struct / Interface / Typedef
Identity // unique id in a repo
FileLine
Content string // struct declaration content

// field type (not include basic types), type name => type id
SubStruct []Identity `json:",omitempty"`

// inline field type (not include basic types)
InlineStruct []Identity `json:",omitempty"`

// methods defined on the Struct, not including inlined type's method
Methods map[string]Identity `json:",omitempty"`

// Implemented interfaces
Implements []Identity `json:",omitempty"`

// functions defined in fields, key is type name, val is the function Signature
// FieldFunctions map[string]string

CompressData *string `json:"compress_data,omitempty"` // struct llm compress result
}

func isUpperCase(c byte) bool {
return c >= 'A' && c <= 'Z'
}

func (p *goParser) parseType(ctx *fileContext, typDecl *ast.TypeSpec, doc string) (st *Type, ct bool) {
switch decl := typDecl.Type.(type) {
case *ast.StructType:
Expand Down
2 changes: 2 additions & 0 deletions src/compress/golang/plugin/parse/go_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"os"
"strings"

. "github.com/cloudwego/abcoder/src/uniast"
)

var (
Expand Down
1 change: 1 addition & 0 deletions src/compress/golang/plugin/parse/go_ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"testing"

. "github.com/cloudwego/abcoder/src/uniast"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/require"
)
Expand Down
18 changes: 2 additions & 16 deletions src/compress/golang/plugin/parse/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"os"
"path/filepath"
"strings"

. "github.com/cloudwego/abcoder/src/uniast"
)

type Parser interface {
Expand Down Expand Up @@ -152,22 +154,6 @@ func (p *goParser) getRepo() Repository {
return p.repo
}

func (p *Module) GetDependency(pkg string) string {
// // search internal library first
// if lib := p.Libraries[mod]; lib != nil {
// return lib
// }
// match the prefix of name for each repo.Dependencies
for k, v := range p.Dependencies {
if strings.HasPrefix(pkg, k) {
return v
}
}
// FIXME: return value's dependency may not explicitly defined in go.mod, thus may not be found
// fmt.Fprintf(os.Stderr, "Error: not found dependency for %v", pkg)
return ""
}

// ToABS converts a local package path to absolute path
// If the path is not a local package, return empty string
// func (p *goParser) pkgPathToABS(path PkgPath) string {
Expand Down
Loading