Skip to content

Commit c8bc49c

Browse files
committed
feat: add version constants and avoid any in kotlin client
1 parent 7987487 commit c8bc49c

10 files changed

Lines changed: 163 additions & 25 deletions

File tree

app.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@ import (
55
"os"
66

77
"github.com/primelib/primecodegen/pkg/cmd"
8+
"github.com/primelib/primecodegen/pkg/constants"
89
)
910

1011
var (
11-
version = "dev"
12+
version = constants.Version
1213
commit = "none"
1314
date = "unknown"
1415
status = "clean"
1516
)
1617

1718
// Init Hook
1819
func init() {
19-
// pass version info the version cmd
20-
cmd.Version = version
21-
cmd.CommitHash = commit
22-
cmd.BuildAt = date
23-
cmd.RepositoryStatus = status
20+
// Set Version Information
21+
constants.Version = version
22+
constants.CommitHash = commit
23+
constants.BuildAt = date
24+
constants.RepositoryStatus = status
2425
}
2526

2627
// CLI Main Entrypoint

pkg/cmd/version.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,19 @@ import (
55
"os"
66
"runtime"
77

8+
"github.com/primelib/primecodegen/pkg/constants"
89
"github.com/spf13/cobra"
910
)
1011

11-
// Version will be set at build time
12-
var Version string
13-
14-
// RepositoryStatus will be set at build time
15-
var RepositoryStatus string
16-
17-
// CommitHash will be set at build time
18-
var CommitHash string
19-
20-
// BuildAt will be set at build time
21-
var BuildAt string
22-
2312
func versionCmd() *cobra.Command {
2413
cmd := &cobra.Command{
2514
Use: "version",
2615
Short: "print version information",
2716
Run: func(cmd *cobra.Command, args []string) {
28-
_, _ = fmt.Fprintf(os.Stdout, "GitVersion: %s\n", Version)
29-
_, _ = fmt.Fprintf(os.Stdout, "GitCommit: %s\n", CommitHash)
30-
_, _ = fmt.Fprintf(os.Stdout, "GitTreeState: %s\n", RepositoryStatus)
31-
_, _ = fmt.Fprintf(os.Stdout, "BuildDate: %s\n", BuildAt)
17+
_, _ = fmt.Fprintf(os.Stdout, "GitVersion: %s\n", constants.Version)
18+
_, _ = fmt.Fprintf(os.Stdout, "GitCommit: %s\n", constants.CommitHash)
19+
_, _ = fmt.Fprintf(os.Stdout, "GitTreeState: %s\n", constants.RepositoryStatus)
20+
_, _ = fmt.Fprintf(os.Stdout, "BuildDate: %s\n", constants.BuildAt)
3221
_, _ = fmt.Fprintf(os.Stdout, "GoVersion: %s\n", runtime.Version())
3322
_, _ = fmt.Fprintf(os.Stdout, "Compiler: %s\n", runtime.Compiler)
3423
_, _ = fmt.Fprintf(os.Stdout, "Platform: %s\n", runtime.GOOS+"/"+runtime.GOARCH)

pkg/constants/version.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package constants
2+
3+
var (
4+
Version = "1.0.0"
5+
CommitHash = "none"
6+
BuildAt = "unknown"
7+
RepositoryStatus = "clean"
8+
)

pkg/openapi/openapigenerator/model.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/pb33f/libopenapi"
1010
"github.com/pb33f/libopenapi/datamodel/high/base"
1111
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
12+
"github.com/primelib/primecodegen/pkg/constants"
1213
"github.com/primelib/primecodegen/pkg/openapi/openapidocument"
1314
"github.com/primelib/primecodegen/pkg/openapi/openapiutil"
1415
"github.com/primelib/primecodegen/pkg/util"
@@ -31,7 +32,7 @@ func BuildTemplateData(doc *libopenapi.DocumentModel[v3.Document], generator Cod
3132
Title: specTitle,
3233
Description: doc.Model.Info.Description,
3334
APISpecVersion: doc.Model.Info.Version,
34-
GeneratorVersion: "1.0.0", // TODO: introduce version constants
35+
GeneratorVersion: constants.Version,
3536
Endpoints: BuildEndpoints(doc),
3637
Auth: BuildAuth(doc),
3738
Packages: packageConfig,

pkg/template/templateapi/functions.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,38 @@ var TemplateFunctions = template.FuncMap{
2828
"hasSuffix": func(s, suffix string) bool {
2929
return strings.HasSuffix(s, suffix)
3030
},
31+
"contains": func(container interface{}, item interface{}) bool {
32+
v := reflect.ValueOf(container)
33+
if !v.IsValid() {
34+
return false
35+
}
36+
37+
switch v.Kind() {
38+
// string contains string
39+
case reflect.String:
40+
itemStr, ok := item.(string)
41+
if !ok {
42+
return false
43+
}
44+
return strings.Contains(v.String(), itemStr)
45+
46+
// slice or array contains item
47+
case reflect.Slice, reflect.Array:
48+
for i := 0; i < v.Len(); i++ {
49+
if reflect.DeepEqual(v.Index(i).Interface(), item) {
50+
return true
51+
}
52+
}
53+
54+
// map contains key
55+
case reflect.Map:
56+
return v.MapIndex(reflect.ValueOf(item)).IsValid()
57+
default:
58+
return false
59+
}
60+
61+
return false
62+
},
3163
"firstNonEmpty": func(values ...string) string {
3264
return util.FirstNonEmptyString(values...)
3365
},

pkg/template/templates/openapi-kotlin-httpclient/aconfig.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,40 @@ var Template = templateapi.Config{
8989
},
9090
// core - model
9191
{
92-
SourceTemplate: "model.gohtml",
92+
SourceTemplate: "model.common.gohtml",
9393
Snippets: templateapi.DefaultSnippets,
9494
TargetDirectory: "core/src/commonMain/kotlin/{{ .Common.Packages.Models | toFilePath }}",
9595
TargetFileName: "{{ .Name }}.kt",
9696
Type: templateapi.TypeModelEach,
9797
Kind: templateapi.KindModel,
9898
},
9999
{
100-
SourceTemplate: "enum.gohtml",
100+
SourceTemplate: "enum.common.gohtml",
101101
Snippets: templateapi.DefaultSnippets,
102102
TargetDirectory: "core/src/commonMain/kotlin/{{ .Common.Packages.Enums | toFilePath }}",
103103
TargetFileName: "{{ .Name }}.kt",
104104
Type: templateapi.TypeEnumEach,
105105
Kind: templateapi.KindModel,
106106
},
107+
// core - model - jvm
108+
/*
109+
{
110+
SourceTemplate: "model.jvm.gohtml",
111+
Snippets: templateapi.DefaultSnippets,
112+
TargetDirectory: "core/src/jvmMain/kotlin/{{ .Common.Packages.Models | toFilePath }}/jvm",
113+
TargetFileName: "{{ .Name }}.kt",
114+
Type: templateapi.TypeModelEach,
115+
Kind: templateapi.KindModel,
116+
},
117+
{
118+
SourceTemplate: "enum.jvm.gohtml",
119+
Snippets: templateapi.DefaultSnippets,
120+
TargetDirectory: "core/src/jvmMain/kotlin/{{ .Common.Packages.Enums | toFilePath }}/jvm",
121+
TargetFileName: "{{ .Name }}.kt",
122+
Type: templateapi.TypeEnumEach,
123+
Kind: templateapi.KindModel,
124+
},
125+
*/
107126
// core - operation response models
108127
{
109128
SourceTemplate: "response.gohtml",

pkg/template/templates/openapi-kotlin-httpclient/enum.gohtml renamed to pkg/template/templates/openapi-kotlin-httpclient/enum.common.gohtml

File renamed without changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{{- /*gotype: github.com/primelib/primecodegen/pkg/openapi/openapigenerator.EnumEachTemplate*/ -}}
2+
{{- template "header-singleline" }}
3+
4+
package {{ .Package }}.jvm;
5+
6+
import com.fasterxml.jackson.annotation.JsonCreator
7+
import com.fasterxml.jackson.annotation.JsonValue
8+
9+
import kotlin.time.Instant
10+
11+
import org.jetbrains.annotations.ApiStatus;
12+
13+
/**
14+
* {{ .Enum.Name }}
15+
{{- if .Enum.Description }}
16+
*
17+
* {{ .Enum.Description | commentMultiLine " * " | escapeJavadoc }}
18+
{{- end }}
19+
*/
20+
{{- if .Enum.Deprecated }}
21+
@Deprecated(message = "{{ if .Enum.DeprecatedReason }}{{ .Enum.DeprecatedReason }}{{ else }}This enum is deprecated{{ end }}")
22+
{{- end }}
23+
enum class {{ .Enum.Name }}(
24+
@get:JsonValue
25+
val value: {{ .Enum.ValueType }}
26+
) {
27+
{{- range $i, $p := .Enum.AllowedValues }}
28+
{{- if $p.Description }}
29+
/**
30+
* {{ $p.Description | commentSingleLine }}
31+
*/
32+
{{- end }}
33+
{{ $p.Name }}({{ if eq $.Enum.ValueType.Type "String" }}"{{ $p.Value }}"{{ else }}{{ $p.Value }}{{ end }}),
34+
{{- end }}
35+
;
36+
37+
companion object {
38+
@JvmStatic
39+
@JsonCreator
40+
fun fromValue(value: {{ $.Enum.ValueType }}): {{ $.Enum.Name }} =
41+
entries.find { it.value == value }
42+
?: throw IllegalArgumentException("Unknown {{ $.Enum.Name }} value: $value")
43+
}
44+
}

pkg/template/templates/openapi-kotlin-httpclient/model.gohtml renamed to pkg/template/templates/openapi-kotlin-httpclient/model.common.gohtml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.jetbrains.annotations.ApiStatus
2828
@Serializable
2929
data class {{ .Model.Name }}(
3030
{{- range $i, $p := .Model.Properties }}
31+
{{- if not (contains $p.Type.Declaration "Any") }}
3132
{{- if $p.Description }}
3233
/**
3334
* {{ $p.Description | escapeJavadoc | commentMultiLine " * " }}
@@ -36,6 +37,7 @@ data class {{ .Model.Name }}(
3637
@SerialName("{{ $p.FieldName }}")
3738
val {{ $p.Name }}: {{ $p.Type.Declaration }}{{ if not $p.Nullable }}? = null{{ end }},
3839
{{- end }}
40+
{{- end }}
3941
){{ if .Model.Parent.Declaration }} : {{ .Model.Parent.Declaration }}(){{ end }} {
4042

4143
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{{- /*gotype: github.com/primelib/primecodegen/pkg/openapi/openapigenerator.ModelEachTemplate*/ -}}
2+
{{- template "header-singleline" }}
3+
4+
package {{ .Package }}.jvm;
5+
6+
import com.fasterxml.jackson.annotation.JsonProperty
7+
import com.fasterxml.jackson.annotation.JsonInclude
8+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
9+
10+
import kotlin.time.Instant
11+
12+
import org.jetbrains.annotations.ApiStatus
13+
14+
/**
15+
* {{ .Model.Name }}
16+
{{- if .Model.Description }}
17+
* <p>
18+
* {{ .Model.Description | escapeJavadoc | commentMultiLine " * " }}
19+
{{- end }}
20+
*
21+
{{- if .Model.Deprecated }}
22+
* @deprecated{{ if .Model.DeprecatedReason }} {{ .Model.DeprecatedReason | escapeJavadoc }}{{ end }}
23+
{{- end }}
24+
*/
25+
{{- if .Model.Deprecated }}
26+
@Deprecated("{{ .Model.DeprecatedReason | escapeJavadoc }}")
27+
{{- end }}
28+
@JsonIgnoreProperties(ignoreUnknown = true)
29+
@JsonInclude(JsonInclude.Include.NON_NULL)
30+
data class {{ .Model.Name }}(
31+
{{- range $i, $p := .Model.Properties }}
32+
{{- if $p.Description }}
33+
/**
34+
* {{ $p.Description | escapeJavadoc | commentMultiLine " * " }}
35+
*/
36+
{{- end }}
37+
@JsonProperty("{{ $p.FieldName }}")
38+
val {{ $p.Name }}: {{ $p.Type.Declaration }}{{ if not $p.Nullable }}? = null{{ end }},
39+
{{- end }}
40+
){{ if .Model.Parent.Declaration }} : {{ .Model.Parent.Declaration }}(){{ end }} {
41+
42+
}

0 commit comments

Comments
 (0)