-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathenum_unknown_error_nested.yml
99 lines (89 loc) · 3.18 KB
/
enum_unknown_error_nested.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
input:
input.go: |
package example
import (
input "github.com/jmattheis/goverter/execution/input"
output "github.com/jmattheis/goverter/execution/output"
)
// goverter:converter
// goverter:enum:unknown @error
type Converter interface {
Convert([]Input) ([]Output, error)
}
type Input struct { Nested NestedInput }
type Output struct { Nested NestedOutput }
type NestedInput struct { Color input.Color }
type NestedOutput struct { Color output.Color }
input/enum.go: |
package input
type Color int
const (
Green Color = iota
Blue
Red
)
output/enum.go: |
package output
type Color string
const (
Green Color = "green"
Blue Color = "blue"
Red Color = "red"
)
success:
- generated/generated.go: |
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
package generated
import (
"fmt"
execution "github.com/jmattheis/goverter/execution"
input "github.com/jmattheis/goverter/execution/input"
output "github.com/jmattheis/goverter/execution/output"
)
type ConverterImpl struct{}
func (c *ConverterImpl) Convert(source []execution.Input) ([]execution.Output, error) {
var exampleOutputList []execution.Output
if source != nil {
exampleOutputList = make([]execution.Output, len(source))
for i := 0; i < len(source); i++ {
exampleOutput, err := c.exampleInputToExampleOutput(source[i])
if err != nil {
return nil, err
}
exampleOutputList[i] = exampleOutput
}
}
return exampleOutputList, nil
}
func (c *ConverterImpl) exampleInputToExampleOutput(source execution.Input) (execution.Output, error) {
var exampleOutput execution.Output
exampleNestedOutput, err := c.exampleNestedInputToExampleNestedOutput(source.Nested)
if err != nil {
return exampleOutput, err
}
exampleOutput.Nested = exampleNestedOutput
return exampleOutput, nil
}
func (c *ConverterImpl) exampleNestedInputToExampleNestedOutput(source execution.NestedInput) (execution.NestedOutput, error) {
var exampleNestedOutput execution.NestedOutput
outputColor, err := c.inputColorToOutputColor(source.Color)
if err != nil {
return exampleNestedOutput, err
}
exampleNestedOutput.Color = outputColor
return exampleNestedOutput, nil
}
func (c *ConverterImpl) inputColorToOutputColor(source input.Color) (output.Color, error) {
var outputColor output.Color
switch source {
case input.Blue:
outputColor = output.Blue
case input.Green:
outputColor = output.Green
case input.Red:
outputColor = output.Red
default:
return outputColor, fmt.Errorf("unexpected enum element: %v", source)
}
return outputColor, nil
}