Skip to content

Commit 36fe172

Browse files
committed
fix output
1 parent 4c67cc9 commit 36fe172

19 files changed

Lines changed: 615 additions & 114 deletions

File tree

internal/writer.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"bytes"
55
"codegen/tmpl"
66
"fmt"
7+
"github.com/samber/lo"
78
"os"
89
"path/filepath"
10+
"strings"
911
"text/template"
1012
)
1113

@@ -33,6 +35,17 @@ type stdWriter struct {
3335
func (w *stdWriter) write(output *tmpl.Output, apis []*tmpl.Api, models []*tmpl.Ref, engine *template.Template) error {
3436
buf := &bytes.Buffer{}
3537

38+
if w.name == "model" {
39+
40+
models = lo.Filter(models, func(item *tmpl.Ref, index int) bool {
41+
return !item.Ignore
42+
})
43+
44+
if len(models) == 0 {
45+
return nil
46+
}
47+
}
48+
3649
data := struct {
3750
Output *tmpl.Output
3851
Env *Env
@@ -51,7 +64,16 @@ func (w *stdWriter) write(output *tmpl.Output, apis []*tmpl.Api, models []*tmpl.
5164
return err
5265
}
5366

54-
file := tmpl.PwdJoinPath(output.File)
67+
//格式化路径参数
68+
textFile := output.File
69+
if len(apis) == 1 {
70+
textFile = strings.ReplaceAll(textFile, "{api}", apis[0].Name)
71+
}
72+
if len(models) == 1 {
73+
textFile = strings.ReplaceAll(textFile, "{model}", models[0].Name)
74+
}
75+
76+
file := tmpl.PwdJoinPath(textFile)
5577
dir := filepath.Dir(file)
5678
err = os.MkdirAll(dir, os.ModePerm)
5779
if err != nil {

tmpl/cpp/api.tmpl

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{{range $val :=.Output.Header}}
2+
{{- $val}}
3+
{{end}}
4+
5+
import com.fasterxml.jackson.core.type.TypeReference;
6+
import com.fasterxml.jackson.databind.JsonNode;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.HashMap;
10+
11+
{{range $val :=.Apis}}
12+
{{template "api" $val}}
13+
{{end}}
14+
15+
16+
{{define "api"}}
17+
/**
18+
* {{.Description}}
19+
*/
20+
public class {{Capitalize .Name}} {
21+
22+
private final ApiClient client;
23+
24+
public {{Capitalize .Name}}(ApiClient client) {
25+
this.client = client;
26+
}
27+
28+
{{range $val :=.Paths -}}
29+
{{template "path" $val}}
30+
{{end}}
31+
}
32+
{{end}}
33+
34+
35+
{{define "path"}}
36+
37+
public static final TypeReference<{{.Response.Expression}}> {{.Name}}ResultType = new TypeReference<{{.Response.Expression}}>() {
38+
};
39+
40+
/**
41+
* {{.Description}}
42+
* {{.Summary}}
43+
*/
44+
public {{.Response.Expression}} {{.Name}}({{template "parameters" .}}) {
45+
{{ if .Queries -}}
46+
Map<String,Object> params = new HashMap<String,Object>();
47+
{{range $idx,$val := .Parameters -}}
48+
params.put("{{$val.Name}}",{{$val.Alias}});
49+
{{end}}
50+
{{- else -}}
51+
{{if .Request}}{{else}}Map<String,Object> params = new HashMap<String,Object>();{{end}}
52+
{{- end}}
53+
return client.{{.Method}}({{.Path}}, {{- if .Request}}body{{else}}params{{end}}, {{.Name}}ResultType);
54+
}
55+
{{end}}
56+
57+
58+
{{define "parameters"}}
59+
{{- range $idx,$val := .Parameters -}}{{if gt $idx 0}},{{end}} {{$val.Type.Expression}} {{$val.Alias}}{{- end -}}
60+
{{end}}

tmpl/cpp/client.tmpl

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
{{define "structHeader"}}
2+
package {{Variable .Variables "structPackage"}};
3+
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
import java.io.Serializable;
10+
import java.util.List;
11+
import java.util.Map;
12+
{{end}}
13+
14+
{{define "struct"}}
15+
16+
/**
17+
* {{.Description}}
18+
* {{.Summary}}
19+
*/
20+
@Data
21+
@NoArgsConstructor
22+
@AllArgsConstructor
23+
public class {{.Type.Expression}} implements Serializable {
24+
{{range $val :=.Properties -}}
25+
/**
26+
* {{.Description}}
27+
* {{.Summary}}
28+
*/
29+
private {{$val.Type.Expression}} {{$val.Name}};
30+
{{end}}
31+
}
32+
{{end}}
33+
34+
35+
{{define "apiHeader"}}
36+
package {{Variable .Variables "apiPackage"}};
37+
38+
import {{Variable .Variables "structPackage"}}.*;
39+
import {{Variable .Variables "clientPackage"}}.ApiClient;
40+
import com.fasterxml.jackson.core.type.TypeReference;
41+
import com.fasterxml.jackson.databind.JsonNode;
42+
import java.util.List;
43+
import java.util.Map;
44+
import java.util.HashMap;
45+
{{end}}
46+
47+
{{define "api"}}
48+
/**
49+
* {{.Description}}
50+
*/
51+
public class {{Capitalize .Name}} {
52+
53+
private final ApiClient client;
54+
55+
public {{Capitalize .Name}}(ApiClient client) {
56+
this.client = client;
57+
}
58+
59+
{{range $val :=.Paths -}}
60+
{{template "path" $val}}
61+
{{end}}
62+
}
63+
{{end}}
64+
65+
66+
67+
{{define "path"}}
68+
69+
public static final TypeReference<{{.Response.Expression}}> {{.Name}}ResultType = new TypeReference<{{.Response.Expression}}>() {
70+
};
71+
72+
/**
73+
* {{.Description}}
74+
* {{.Summary}}
75+
*/
76+
public {{.Response.Expression}} {{.Name}}({{template "parameters" .}}) {
77+
{{ if .Queries -}}
78+
Map<String,Object> params = new HashMap<String,Object>();
79+
{{range $idx,$val := .Parameters -}}
80+
params.put("{{$val.Name}}",{{$val.Alias}});
81+
{{end}}
82+
{{- else -}}
83+
{{if .Request}}{{else}}Map<String,Object> params = new HashMap<String,Object>();{{end}}
84+
{{- end}}
85+
return client.{{.Method}}({{.Path}}, {{- if .Request}}body{{else}}params{{end}}, {{.Name}}ResultType);
86+
}
87+
{{end}}
88+
89+
90+
91+
92+
{{define "parameters"}}
93+
{{- range $idx,$val := .Parameters -}}{{if gt $idx 0}},{{end}} {{$val.Type.Expression}} {{$val.Alias}}{{- end -}}
94+
{{end}}
95+
96+
97+
{{define "client"}}
98+
99+
package {{Variable .Variables "clientPackage"}};
100+
101+
import com.fasterxml.jackson.core.type.TypeReference;
102+
103+
import java.util.Map;
104+
105+
public interface ApiClient {
106+
107+
<T> T get(String path, Map<String, Object> params, TypeReference<T> resultType);
108+
109+
<P, T> T put(String path, P body, TypeReference<T> resultType);
110+
111+
<P, T> T post(String path, P params, TypeReference<T> resultType);
112+
113+
<P, T> T delete(String path, P params, TypeReference<T> resultType);
114+
}
115+
{{end}}

tmpl/cpp/cpp.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package cpp
2+
3+
import (
4+
"codegen/lang"
5+
_ "embed"
6+
"fmt"
7+
"strings"
8+
)
9+
10+
//go:embed api.tmpl
11+
var templateApi string
12+
13+
//go:embed client.tmpl
14+
var templateClient string
15+
16+
//go:embed model.tmpl
17+
var templateModel string
18+
19+
var Templates = map[string]string{
20+
"api": templateApi,
21+
"model": templateModel,
22+
"client": templateClient,
23+
}
24+
25+
type Convert struct {
26+
}
27+
28+
func (j *Convert) Reference(name string) string {
29+
return name
30+
}
31+
32+
func (j *Convert) Foundation(name string, format string) string {
33+
switch name {
34+
case "integer", "number":
35+
if format == "int64" {
36+
return "Long"
37+
}
38+
return "Integer"
39+
case "string":
40+
return "String"
41+
case "boolean":
42+
return "Boolean"
43+
case "object":
44+
return "JsonNode"
45+
default:
46+
return name
47+
}
48+
}
49+
50+
func (j *Convert) Map(sub string) string {
51+
return fmt.Sprintf("Map<String,%s>", sub)
52+
}
53+
54+
func (j *Convert) Array(sub string) string {
55+
return fmt.Sprintf("List<%s>", sub)
56+
}
57+
58+
func (j *Convert) Generic(parentType string, mode lang.GenericMode, subTypes ...string) string {
59+
return fmt.Sprintf("%s<%s>", parentType, strings.Join(subTypes, ","))
60+
}

tmpl/cpp/model.tmpl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{{range $val :=.Output.Header}}
2+
{{- $val}}
3+
{{end}}
4+
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
7+
import java.io.Serializable;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
{{range $val :=.Models}}
12+
{{template "model" $val}}
13+
{{end}}
14+
15+
16+
{{define "model"}}
17+
18+
/**
19+
* {{.Description}}
20+
* {{.Summary}}
21+
*/
22+
public class {{.Type.Expression}} implements Serializable {
23+
{{range $val :=.Properties -}}
24+
/**
25+
* {{.Description}}
26+
* {{.Summary}}
27+
*/
28+
private {{$val.Type.Expression}} {{$val.Name}};
29+
{{end}}
30+
31+
{{range $val :=.Properties}}
32+
public {{$val.Type.Expression}} get{{Capitalize $val.Name}}() {
33+
return {{$val.Name}};
34+
}
35+
36+
public void set{{Capitalize $val.Name}}({{$val.Type.Expression}} {{$val.Name}}) {
37+
this.{{$val.Name}} = {{$val.Name}};
38+
}
39+
{{end}}
40+
}
41+
{{end}}

tmpl/golang/model.tmpl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
{{- $val}}
33
{{end}}
44

5-
import "fmt"
6-
75
{{range $val :=.Models}}
86
{{template "model" $val}}
97
{{end}}

0 commit comments

Comments
 (0)