Skip to content

Commit cc80bed

Browse files
lvan100lianghuan
authored andcommitted
feat(gen): refactoring gs-http-gen
1 parent 8afe8d7 commit cc80bed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+6389
-598
lines changed

gen/gen.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 The Go-Spring Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package gen
218

319
import (
@@ -8,19 +24,20 @@ import (
824

925
"github.com/go-spring/gs-http-gen/gen/generator"
1026
"github.com/go-spring/gs-http-gen/gen/generator/golang"
11-
"github.com/go-spring/gs-http-gen/lib/parser"
27+
"github.com/go-spring/gs-http-gen/lib/tidl"
1228
)
1329

1430
func init() {
1531
generator.RegisterGenerator("go", &golang.Generator{})
1632
}
1733

34+
// Gen is the entry point for generating code for the given language.
1835
func Gen(language string, config *generator.Config) error {
1936
g, ok := generator.GetGenerator(language)
2037
if !ok {
2138
return fmt.Errorf("unsupported language: %s", language)
2239
}
23-
files, meta, err := parse(config.IDLDir)
40+
files, meta, err := parse(config.IDLSrcDir)
2441
if err != nil {
2542
return err
2643
}
@@ -30,14 +47,12 @@ func Gen(language string, config *generator.Config) error {
3047
if len(files) == 0 {
3148
return fmt.Errorf("no idl file")
3249
}
33-
if err = parser.Verify(files, meta); err != nil {
34-
return err
35-
}
3650
return g.Gen(config, files, meta)
3751
}
3852

39-
func parse(dir string) (files map[string]parser.Document, meta *parser.MetaInfo, err error) {
40-
files = make(map[string]parser.Document)
53+
// parse scans the given directory for IDL files and the meta.json file.
54+
func parse(dir string) (files map[string]tidl.Document, meta *tidl.MetaInfo, err error) {
55+
files = make(map[string]tidl.Document)
4156
entries, err := os.ReadDir(dir)
4257
if err != nil {
4358
return nil, nil, err
@@ -48,18 +63,20 @@ func parse(dir string) (files map[string]parser.Document, meta *parser.MetaInfo,
4863
}
4964
fileName := e.Name()
5065

66+
// Parse meta.json file if found
5167
if fileName == "meta.json" {
5268
if meta, err = parseMeta(dir, fileName); err != nil {
5369
return nil, nil, err
5470
}
5571
continue
5672
}
5773

74+
// Skip non-IDL files
5875
if !strings.HasSuffix(fileName, ".idl") {
5976
continue
6077
}
6178

62-
var doc parser.Document
79+
var doc tidl.Document
6380
doc, err = parseFile(dir, fileName)
6481
if err != nil {
6582
return nil, nil, err
@@ -69,20 +86,22 @@ func parse(dir string) (files map[string]parser.Document, meta *parser.MetaInfo,
6986
return
7087
}
7188

72-
func parseMeta(dir string, fileName string) (*parser.MetaInfo, error) {
89+
// parseMeta reads and parses the meta.json file to extract service metadata.
90+
func parseMeta(dir string, fileName string) (*tidl.MetaInfo, error) {
7391
fileName = filepath.Join(dir, fileName)
7492
b, err := os.ReadFile(fileName)
7593
if err != nil {
7694
return nil, err
7795
}
78-
return parser.ParseMeta(string(b))
96+
return tidl.ParseMeta(string(b))
7997
}
8098

81-
func parseFile(dir string, fileName string) (parser.Document, error) {
99+
// parseFile reads and parses a single IDL file into a document.
100+
func parseFile(dir string, fileName string) (tidl.Document, error) {
82101
fileName = filepath.Join(dir, fileName)
83102
b, err := os.ReadFile(fileName)
84103
if err != nil {
85-
return parser.Document{}, err
104+
return tidl.Document{}, err
86105
}
87-
return parser.Parse(string(b))
106+
return tidl.Parse(string(b))
88107
}

gen/gen_test.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 The Go-Spring Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package gen
218

319
import (
@@ -41,15 +57,15 @@ func testProject(t *testing.T, dir string) {
4157
idlDir := filepath.Join(dir, "idl")
4258
for lang, c := range m {
4359
outDir := filepath.Join(dir, lang, "proto")
44-
os.RemoveAll(outDir)
45-
os.Mkdir(outDir, os.ModePerm)
60+
_ = os.RemoveAll(outDir)
61+
_ = os.Mkdir(outDir, os.ModePerm)
4662
config := &generator.Config{
47-
IDLDir: idlDir,
48-
OutDir: outDir,
49-
Version: "v0.0.0",
50-
Server: c.Server,
51-
Client: c.Client,
52-
PkgName: "proto",
63+
IDLSrcDir: idlDir,
64+
OutputDir: outDir,
65+
EnableServer: c.Server,
66+
EnableClient: c.Client,
67+
PackageName: "proto",
68+
ToolVersion: "v0.0.1",
5369
}
5470
if err = Gen(lang, config); err != nil {
5571
t.Fatal(err)

gen/generator/generator.go

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,91 @@
1+
/*
2+
* Copyright 2025 The Go-Spring Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package generator
218

319
import (
420
"fmt"
5-
"strings"
621

7-
"github.com/go-spring/gs-http-gen/lib/parser"
22+
"github.com/go-spring/gs-http-gen/lib/tidl"
823
)
924

25+
// Config holds the configuration options for the code generator.
1026
type Config struct {
11-
IDLDir string
12-
OutDir string
13-
Version string
14-
Server bool
15-
Client bool
16-
PkgName string
27+
IDLSrcDir string // Directory containing source IDL files
28+
OutputDir string // Directory where generated code will be written
29+
EnableServer bool // Whether to generate server code
30+
EnableClient bool // Whether to generate client code
31+
PackageName string // Go package name for generated code
32+
ToolVersion string // Version of the code generation tool
1733
}
1834

1935
var generators = map[string]Generator{}
2036

37+
// Generator defines the interface that any language-specific generator
38+
// must implement. The Gen method generates code based on the given
39+
// configuration, documents, and metadata.
2140
type Generator interface {
22-
Gen(config *Config, files map[string]parser.Document, meta *parser.MetaInfo) error
41+
Gen(config *Config, files map[string]tidl.Document, meta *tidl.MetaInfo) error
2342
}
2443

44+
// GetGenerator retrieves a registered generator for a given language.
2545
func GetGenerator(language string) (Generator, bool) {
2646
g, ok := generators[language]
2747
return g, ok
2848
}
2949

50+
// RegisterGenerator registers a new generator for the given language.
3051
func RegisterGenerator(language string, g Generator) {
3152
if _, ok := generators[language]; ok {
3253
panic(fmt.Errorf("duplicate generator for %s", language))
3354
}
3455
generators[language] = g
3556
}
3657

37-
func GetType(files map[string]parser.Document, name string) *parser.Type {
58+
// GetEnum searches all documents for an enum type with the given name.
59+
func GetEnum(files map[string]tidl.Document, name string) (tidl.Enum, bool) {
3860
for _, doc := range files {
39-
for _, t := range doc.Types {
40-
if t.Name == name {
41-
return t
61+
for _, e := range doc.Enums {
62+
if CapitalizeASCII(e.Name) == name {
63+
return e, true
4264
}
4365
}
4466
}
45-
return nil
67+
return tidl.Enum{}, false
4668
}
4769

48-
func GetAnnotation(arr []parser.Annotation, name string) (parser.Annotation, bool) {
49-
for _, a := range arr {
50-
if a.Key == name {
51-
return a, true
70+
// GetType searches all documents for a type with the given name.
71+
func GetType(files map[string]tidl.Document, name string) (tidl.Type, bool) {
72+
for _, doc := range files {
73+
for _, t := range doc.Types {
74+
if CapitalizeASCII(t.Name) == name {
75+
return t, true
76+
}
5277
}
5378
}
54-
return parser.Annotation{}, false
79+
return tidl.Type{}, false
5580
}
5681

57-
func ToPascal(s string) string {
58-
var sb strings.Builder
59-
parts := strings.Split(s, "_")
60-
for _, part := range parts {
61-
if part == "" {
62-
continue
63-
}
64-
c := part[0]
65-
if 'a' <= c && c <= 'z' {
66-
c = c - 'a' + 'A'
67-
}
68-
sb.WriteByte(c)
69-
if len(part) > 1 {
70-
sb.WriteString(part[1:])
71-
}
82+
// CapitalizeASCII capitalizes the first ASCII letter of a string.
83+
func CapitalizeASCII(s string) string {
84+
if len(s) == 0 {
85+
return s
86+
}
87+
if s[0] >= 'a' && s[0] <= 'z' {
88+
return string(s[0]-'a'+'A') + s[1:]
7289
}
73-
return sb.String()
90+
return s
7491
}

gen/generator/golang/client.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
/*
2+
* Copyright 2025 The Go-Spring Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package golang
218

319
import (
4-
"github.com/go-spring/gs-http-gen/lib/parser"
20+
"github.com/go-spring/gs-http-gen/lib/tidl"
521
)
622

7-
func (g *Generator) genClient(ctx Context, rpcs []*parser.RPC) error {
23+
// genClient generates the HTTP client code for a given service.
24+
func (g *Generator) genClient(ctx Context, rpcs []tidl.RPC) error {
825
return nil
926
}

0 commit comments

Comments
 (0)