Skip to content

Commit

Permalink
Merge pull request #33 from chans-open-source/dubbogo-hessian-registry
Browse files Browse the repository at this point in the history
add dubbo-go-hessian-registry generator
  • Loading branch information
cjphaha authored Feb 6, 2022
2 parents 90eb50d + 5fce228 commit 08d6657
Show file tree
Hide file tree
Showing 10 changed files with 759 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The tool should support dubbo protocol. It makes it easy for you to define your
### 3. How to get dubbo
`go get -u github.com/dubbogo/tools/cmd/protoc-gen-dubbo`

### 4. How to get dubbo
### 4. How to get dubbo3
`go get -u github.com/dubbogo/tools/cmd/protoc-gen-dubbo3`

### 5. How to get imports-formatter
Expand Down
33 changes: 33 additions & 0 deletions cmd/dubbogo-cli/common/interfaces.go
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
)
108 changes: 108 additions & 0 deletions cmd/dubbogo-cli/generator/README.md
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 |
98 changes: 98 additions & 0 deletions cmd/dubbogo-cli/generator/adapter.go
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
}
40 changes: 40 additions & 0 deletions cmd/dubbogo-cli/generator/constant.go
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"
)
Loading

0 comments on commit 08d6657

Please sign in to comment.