Skip to content

Commit 2befa5b

Browse files
committed
gentypes: option to force generate optional types
Closes #TNTP-3734.
1 parent 93a7028 commit 2befa5b

File tree

6 files changed

+270
-1
lines changed

6 files changed

+270
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Flags:
7070
`-ext-code`: MessagePack extension code to use for custom types (must be between
7171
-128 and 127, no default value)
7272
`-verbose`: Enable verbose output (default: `false`)
73+
`-force`: Ignore absence of marshal/unmarshal methods on type (default: `false`)
7374

7475
#### Using Generated Types
7576

cmd/gentypes/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
//go:generate go run github.com/tarantool/go-option/cmd/gentypes -ext-code 1 -package test FullMsgpackExtType
2+
//go:generate go run github.com/tarantool/go-option/cmd/gentypes -ext-code 2 -force -package test HiddenTypeAlias
23
package main

cmd/gentypes/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
packagePath string
2828
extCode int
2929
verbose bool
30+
force bool
3031
)
3132

3233
func logfuncf(format string, args ...interface{}) {
@@ -106,6 +107,7 @@ func main() { //nolint:funlen
106107
flag.StringVar(&packagePath, "package", "./", "input and output path")
107108
flag.IntVar(&extCode, "ext-code", undefinedExtCode, "extension code")
108109
flag.BoolVar(&verbose, "verbose", false, "print verbose output")
110+
flag.BoolVar(&force, "force", false, "generate files even if methods do not exist")
109111

110112
flag.Parse()
111113

@@ -171,7 +173,10 @@ func main() { //nolint:funlen
171173

172174
fmt.Println("generating optional for:", typeName)
173175

174-
if !typeSpecDef.HasMethod("MarshalMsgpack") || !typeSpecDef.HasMethod("UnmarshalMsgpack") {
176+
switch {
177+
case force:
178+
// Skipping check for MarshalMsgpack and UnmarshalMsgpack methods.
179+
case !typeSpecDef.HasMethod("MarshalMsgpack") || !typeSpecDef.HasMethod("UnmarshalMsgpack"):
175180
fmt.Println("failed to find MarshalMsgpack or UnmarshalMsgpack method for struct:", typeName)
176181
os.Exit(1)
177182
}

cmd/gentypes/test/hiddentypealias.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package test
2+
3+
import (
4+
"github.com/tarantool/go-option/cmd/gentypes/test/subpackage"
5+
)
6+
7+
type HiddenTypeAlias = subpackage.Hidden

cmd/gentypes/test/hiddentypealias_gen.go

Lines changed: 241 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package subpackage
2+
3+
type Hidden struct {
4+
Hidden string
5+
}
6+
7+
func (h *Hidden) MarshalMsgpack() ([]byte, error) {
8+
return []byte(h.Hidden), nil
9+
}
10+
11+
func (h *Hidden) UnmarshalMsgpack(bytes []byte) error {
12+
h.Hidden = string(bytes)
13+
return nil
14+
}

0 commit comments

Comments
 (0)