-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from chans-open-source/dubbogo-hessian-registry
add dubbo-go-hessian-registry generator
- Loading branch information
Showing
10 changed files
with
759 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 | ||
* | ||
* http://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 common | ||
|
||
type Adapter interface { | ||
CheckParam() bool | ||
|
||
Execute() | ||
|
||
GetMode() AdapterMode | ||
} | ||
|
||
type AdapterMode int | ||
|
||
const ( | ||
TelnetAdapterMode AdapterMode = iota | ||
GeneratorAdapterMode | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# dubbogo-hessian-registry | ||
|
||
Auxiliary tools for [dubbo-go](https://github.com/apache/dubbo-go). | ||
|
||
Automatic generate hessian.POJO registry statement. | ||
|
||
## Install | ||
|
||
```shell | ||
go get -u github.com/dubbogo/tools/cmd/dubbogo-cli | ||
``` | ||
|
||
## Usage | ||
|
||
### main.go | ||
|
||
```go | ||
package main | ||
|
||
//go:generate go run "github.com/dubbogo/tools/cmd/dubbogo-cli" -generator -include pkg | ||
func main() { | ||
|
||
} | ||
|
||
``` | ||
|
||
### pkg/demo.go | ||
|
||
```go | ||
package pkg | ||
|
||
type Demo0 struct { | ||
A string `json:"a"` | ||
B string `json:"b"` | ||
} | ||
|
||
func (*Demo0) JavaClassName() string { | ||
return "org.apache.dubbo.Demo0" | ||
} | ||
|
||
type Demo1 struct { | ||
C string `json:"c"` | ||
} | ||
|
||
func (*Demo1) JavaClassName() string { | ||
return "org.apache.dubbo.Demo1" | ||
} | ||
|
||
``` | ||
|
||
### Execute `go generate` | ||
|
||
```shell | ||
go generate | ||
``` | ||
|
||
### Console logs | ||
```shell | ||
2022/01/28 11:58:11 === Generate start [pkg\demo.go] === | ||
2022/01/28 11:58:11 === Registry POJO [pkg\demo.go].Demo0 === | ||
2022/01/28 11:58:11 === Registry POJO [pkg\demo.go].Demo1 === | ||
2022/01/28 11:58:11 === Generate completed [pkg\demo.go] === | ||
``` | ||
|
||
### Result | ||
|
||
pkg/demo.go | ||
|
||
```go | ||
package pkg | ||
|
||
import ( | ||
"github.com/apache/dubbo-go-hessian2" | ||
) | ||
|
||
type Demo0 struct { | ||
A string `json:"a"` | ||
B string `json:"b"` | ||
} | ||
|
||
func (*Demo0) JavaClassName() string { | ||
return "org.apache.dubbo.Demo0" | ||
} | ||
|
||
type Demo1 struct { | ||
C string `json:"c"` | ||
} | ||
|
||
func (*Demo1) JavaClassName() string { | ||
return "org.apache.dubbo.Demo1" | ||
} | ||
|
||
func init() { | ||
|
||
hessian.RegisterPOJO(&Demo0{}) | ||
|
||
hessian.RegisterPOJO(&Demo1{}) | ||
|
||
} | ||
|
||
``` | ||
|
||
## Command flags | ||
|
||
| flag | description | default | | ||
|:-------:|:---------------------------------------:|:--------------:| | ||
| include | Preprocess files parent directory path. | ./ | | ||
| thread | Worker thread limit. | (cpu core) * 2 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 | ||
* | ||
* http://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 generator | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
import ( | ||
"github.com/dubbogo/tools/cmd/dubbogo-cli/common" | ||
) | ||
|
||
type GeneratorAdapter struct { | ||
DirPath string // 文件扫描目录 | ||
ThreadLimit int // 最大工作线程数 | ||
} | ||
|
||
func (a *GeneratorAdapter) CheckParam() bool { | ||
if a.ThreadLimit < 1 { | ||
a.ThreadLimit = 1 | ||
} | ||
return true | ||
} | ||
|
||
func (a *GeneratorAdapter) Execute() { | ||
fileList, err := listFiles(a.DirPath, targetFileSuffix) | ||
if err != nil { | ||
showLog(errorLog, "%v", err) | ||
return | ||
} | ||
|
||
pool := NewPool(a.ThreadLimit) | ||
for _, f := range fileList { | ||
pool.Execute(NewGenerator(f)) | ||
} | ||
pool.Wait() | ||
} | ||
|
||
func (a *GeneratorAdapter) GetMode() common.AdapterMode { | ||
return common.GeneratorAdapterMode | ||
} | ||
|
||
type fileInfo struct { | ||
path string | ||
|
||
packageStartIndex int | ||
packageEndIndex int | ||
|
||
hasInitFunc bool | ||
initFuncStartIndex int | ||
initFuncEndIndex int | ||
initFuncStatementStartIndex int | ||
|
||
hasHessianImport bool | ||
|
||
buffer []byte | ||
|
||
hessianPOJOList [][]byte | ||
} | ||
|
||
// listFiles 获取目标目录下所有go文件 | ||
func listFiles(dirPath string, suffix string) (fileList []string, err error) { | ||
suffix = strings.ToUpper(suffix) | ||
fileList = make([]string, 0) | ||
_, err = os.Lstat(dirPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("找不到该目录[%s]", dirPath) | ||
} | ||
err = filepath.WalkDir(dirPath, func(path string, d fs.DirEntry, err error) error { // 递归获取目录下所有go文件 | ||
if d == nil || d.IsDir() { | ||
return nil | ||
} | ||
if strings.HasSuffix(strings.ToUpper(d.Name()), suffix) { | ||
fileList = append(fileList, path) | ||
} | ||
return nil | ||
}) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 | ||
* | ||
* http://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 generator | ||
|
||
const ( | ||
PackageRegexp = `^package\s[a-zA-Z_][0-9a-zA-Z_]*$` | ||
|
||
LineCommentRegexp = `\/\/` | ||
MutLineCommentStartRegexp = `\/\*` | ||
MutLineCommentEndRegexp = `\*\/` | ||
|
||
InitFunctionRegexp = `^func\sinit\(\)\s\{$` | ||
|
||
HessianImportRegexp = `"github.com/apache/dubbo-go-hessian2"` | ||
|
||
HessianPOJORegexp = `\*[0-9a-zA-Z_]+\)\sJavaClassName\(\)\sstring\s\{$` | ||
HessianPOJONameRegexp = `\*[0-9a-zA-Z_]+\)` | ||
) | ||
|
||
const ( | ||
newLine byte = '\n' | ||
funcEnd byte = '}' | ||
|
||
targetFileSuffix = ".go" | ||
) |
Oops, something went wrong.