feat: use go:embed to embed templates (#1756)
This commit is contained in:
9
tools/goctl/api/javagen/bool.tpl
Normal file
9
tools/goctl/api/javagen/bool.tpl
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
{{.indent}}{{.decorator}}
|
||||
{{.indent}}public {{.returnType}} is{{.property}}() {
|
||||
{{.indent}} return this.{{.tagValue}};
|
||||
{{.indent}}}
|
||||
|
||||
{{.indent}}public void set{{.property}}({{.type}} {{.propertyValue}}) {
|
||||
{{.indent}} this.{{.tagValue}} = {{.propertyValue}};
|
||||
{{.indent}}}
|
||||
22
tools/goctl/api/javagen/component.tpl
Normal file
22
tools/goctl/api/javagen/component.tpl
Normal file
@@ -0,0 +1,22 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package com.xhb.logic.http.packet.{{.packet}}.model;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
{{.imports}}
|
||||
|
||||
public class {{.className}} extends {{.superClassName}} {
|
||||
|
||||
{{.properties}}
|
||||
{{if .HasProperty}}
|
||||
|
||||
public {{.className}}() {
|
||||
}
|
||||
|
||||
public {{.className}}({{.params}}) {
|
||||
{{.constructorSetter}}
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{.getSet}}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
// JavaCommand the generate java code command entrance
|
||||
// JavaCommand generates java code command entrance.
|
||||
func JavaCommand(c *cli.Context) error {
|
||||
apiFile := c.String("api")
|
||||
dir := c.String("dir")
|
||||
|
||||
@@ -3,6 +3,7 @@ package javagen
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -18,54 +19,19 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
componentTemplate = `// Code generated by goctl. DO NOT EDIT.
|
||||
package com.xhb.logic.http.packet.{{.packet}}.model;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
{{.imports}}
|
||||
|
||||
public class {{.className}} extends {{.superClassName}} {
|
||||
|
||||
{{.properties}}
|
||||
{{if .HasProperty}}
|
||||
|
||||
public {{.className}}() {
|
||||
}
|
||||
|
||||
public {{.className}}({{.params}}) {
|
||||
{{.constructorSetter}}
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{.getSet}}
|
||||
}
|
||||
`
|
||||
getSetTemplate = `
|
||||
{{.indent}}{{.decorator}}
|
||||
{{.indent}}public {{.returnType}} get{{.property}}() {
|
||||
{{.indent}} return this.{{.tagValue}};
|
||||
{{.indent}}}
|
||||
|
||||
{{.indent}}public void set{{.property}}({{.type}} {{.propertyValue}}) {
|
||||
{{.indent}} this.{{.tagValue}} = {{.propertyValue}};
|
||||
{{.indent}}}
|
||||
`
|
||||
|
||||
boolTemplate = `
|
||||
{{.indent}}{{.decorator}}
|
||||
{{.indent}}public {{.returnType}} is{{.property}}() {
|
||||
{{.indent}} return this.{{.tagValue}};
|
||||
{{.indent}}}
|
||||
|
||||
{{.indent}}public void set{{.property}}({{.type}} {{.propertyValue}}) {
|
||||
{{.indent}} this.{{.tagValue}} = {{.propertyValue}};
|
||||
{{.indent}}}
|
||||
`
|
||||
httpResponseData = "import com.xhb.core.response.HttpResponseData;"
|
||||
httpData = "import com.xhb.core.packet.HttpData;"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed component.tpl
|
||||
componentTemplate string
|
||||
//go:embed getset.tpl
|
||||
getSetTemplate string
|
||||
//go:embed bool.tpl
|
||||
boolTemplate string
|
||||
)
|
||||
|
||||
type componentsContext struct {
|
||||
api *spec.ApiSpec
|
||||
requestTypes []spec.Type
|
||||
|
||||
@@ -2,6 +2,7 @@ package javagen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"strings"
|
||||
"text/template"
|
||||
@@ -12,32 +13,8 @@ import (
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util"
|
||||
)
|
||||
|
||||
const packetTemplate = `package com.xhb.logic.http.packet.{{.packet}};
|
||||
|
||||
import com.xhb.core.packet.HttpPacket;
|
||||
import com.xhb.core.network.HttpRequestClient;
|
||||
{{.imports}}
|
||||
|
||||
{{.doc}}
|
||||
public class {{.packetName}} extends HttpPacket<{{.responseType}}> {
|
||||
{{.paramsDeclaration}}
|
||||
|
||||
public {{.packetName}}({{.params}}{{if .HasRequestBody}}{{.requestType}} request{{end}}) {
|
||||
{{if .HasRequestBody}}super(request);{{else}}super(EmptyRequest.instance);{{end}}
|
||||
{{if .HasRequestBody}}this.request = request;{{end}}{{.paramsSetter}}
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpRequestClient.Method requestMethod() {
|
||||
return HttpRequestClient.Method.{{.method}};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String requestUri() {
|
||||
return {{.uri}};
|
||||
}
|
||||
}
|
||||
`
|
||||
//go:embed packet.tpl
|
||||
var packetTemplate string
|
||||
|
||||
func genPacket(dir, packetName string, api *spec.ApiSpec) error {
|
||||
for _, route := range api.Service.Routes() {
|
||||
|
||||
9
tools/goctl/api/javagen/getset.tpl
Normal file
9
tools/goctl/api/javagen/getset.tpl
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
{{.indent}}{{.decorator}}
|
||||
{{.indent}}public {{.returnType}} get{{.property}}() {
|
||||
{{.indent}} return this.{{.tagValue}};
|
||||
{{.indent}}}
|
||||
|
||||
{{.indent}}public void set{{.property}}({{.type}} {{.propertyValue}}) {
|
||||
{{.indent}} this.{{.tagValue}} = {{.propertyValue}};
|
||||
{{.indent}}}
|
||||
25
tools/goctl/api/javagen/packet.tpl
Normal file
25
tools/goctl/api/javagen/packet.tpl
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.xhb.logic.http.packet.{{.packet}};
|
||||
|
||||
import com.xhb.core.packet.HttpPacket;
|
||||
import com.xhb.core.network.HttpRequestClient;
|
||||
{{.imports}}
|
||||
|
||||
{{.doc}}
|
||||
public class {{.packetName}} extends HttpPacket<{{.responseType}}> {
|
||||
{{.paramsDeclaration}}
|
||||
|
||||
public {{.packetName}}({{.params}}{{if .HasRequestBody}}{{.requestType}} request{{end}}) {
|
||||
{{if .HasRequestBody}}super(request);{{else}}super(EmptyRequest.instance);{{end}}
|
||||
{{if .HasRequestBody}}this.request = request;{{end}}{{.paramsSetter}}
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpRequestClient.Method requestMethod() {
|
||||
return HttpRequestClient.Method.{{.method}};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String requestUri() {
|
||||
return {{.uri}};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user