|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import "fmt" |
| 18 | + |
| 19 | +// A converter generates code to convert between a proto type and a veneer type. |
| 20 | +type converter interface { |
| 21 | + // genFrom returns code to convert from proto to veneer. |
| 22 | + genFrom(string) string |
| 23 | + // genTo returns code to convert to proto from veneer. |
| 24 | + genTo(string) string |
| 25 | + // These return the function argument to Transform{Slice,MapValues}, or "" if we don't need it. |
| 26 | + genTransformFrom() string |
| 27 | + genTransformTo() string |
| 28 | +} |
| 29 | + |
| 30 | +// An identityConverter does no conversion. |
| 31 | +type identityConverter struct{} |
| 32 | + |
| 33 | +func (identityConverter) genFrom(arg string) string { return arg } |
| 34 | +func (identityConverter) genTo(arg string) string { return arg } |
| 35 | +func (identityConverter) genTransformFrom() string { return "" } |
| 36 | +func (identityConverter) genTransformTo() string { return "" } |
| 37 | + |
| 38 | +// A derefConverter converts between T in the veneer and *T in the proto. |
| 39 | +type derefConverter struct{} |
| 40 | + |
| 41 | +func (derefConverter) genFrom(arg string) string { return fmt.Sprintf("support.DerefOrZero(%s)", arg) } |
| 42 | +func (derefConverter) genTo(arg string) string { return fmt.Sprintf("support.AddrOrNil(%s)", arg) } |
| 43 | +func (derefConverter) genTransformFrom() string { panic("can't handle deref slices") } |
| 44 | +func (derefConverter) genTransformTo() string { panic("can't handle deref slices") } |
| 45 | + |
| 46 | +type enumConverter struct { |
| 47 | + protoName, veneerName string |
| 48 | +} |
| 49 | + |
| 50 | +func (c enumConverter) genFrom(arg string) string { |
| 51 | + return fmt.Sprintf("%s(%s)", c.veneerName, arg) |
| 52 | +} |
| 53 | + |
| 54 | +func (c enumConverter) genTransformFrom() string { |
| 55 | + return fmt.Sprintf("func(p pb.%s) %s { return %s }", c.protoName, c.veneerName, c.genFrom("p")) |
| 56 | +} |
| 57 | + |
| 58 | +func (c enumConverter) genTo(arg string) string { |
| 59 | + return fmt.Sprintf("pb.%s(%s)", c.protoName, arg) |
| 60 | +} |
| 61 | + |
| 62 | +func (c enumConverter) genTransformTo() string { |
| 63 | + return fmt.Sprintf("func(v %s) pb.%s { return %s }", c.veneerName, c.protoName, c.genTo("v")) |
| 64 | +} |
| 65 | + |
| 66 | +type protoConverter struct { |
| 67 | + veneerName string |
| 68 | +} |
| 69 | + |
| 70 | +func (c protoConverter) genFrom(arg string) string { |
| 71 | + return fmt.Sprintf("(%s{}).fromProto(%s)", c.veneerName, arg) |
| 72 | +} |
| 73 | + |
| 74 | +func (c protoConverter) genTransformFrom() string { |
| 75 | + return fmt.Sprintf("(%s{}).fromProto", c.veneerName) |
| 76 | +} |
| 77 | + |
| 78 | +func (c protoConverter) genTo(arg string) string { |
| 79 | + return fmt.Sprintf("%s.toProto()", arg) |
| 80 | +} |
| 81 | + |
| 82 | +func (c protoConverter) genTransformTo() string { |
| 83 | + return fmt.Sprintf("(*%s).toProto", c.veneerName) |
| 84 | +} |
| 85 | + |
| 86 | +type customConverter struct { |
| 87 | + toFunc, fromFunc string |
| 88 | +} |
| 89 | + |
| 90 | +func (c customConverter) genFrom(arg string) string { |
| 91 | + return fmt.Sprintf("%s(%s)", c.fromFunc, arg) |
| 92 | +} |
| 93 | + |
| 94 | +func (c customConverter) genTransformFrom() string { return c.fromFunc } |
| 95 | + |
| 96 | +func (c customConverter) genTo(arg string) string { |
| 97 | + return fmt.Sprintf("%s(%s)", c.toFunc, arg) |
| 98 | +} |
| 99 | + |
| 100 | +func (c customConverter) genTransformTo() string { return c.toFunc } |
| 101 | + |
| 102 | +type sliceConverter struct { |
| 103 | + eltConverter converter |
| 104 | +} |
| 105 | + |
| 106 | +func (c sliceConverter) genFrom(arg string) string { |
| 107 | + if fn := c.eltConverter.genTransformFrom(); fn != "" { |
| 108 | + return fmt.Sprintf("support.TransformSlice(%s, %s)", arg, fn) |
| 109 | + } |
| 110 | + return c.eltConverter.genFrom(arg) |
| 111 | +} |
| 112 | + |
| 113 | +func (c sliceConverter) genTo(arg string) string { |
| 114 | + if fn := c.eltConverter.genTransformTo(); fn != "" { |
| 115 | + return fmt.Sprintf("support.TransformSlice(%s, %s)", arg, fn) |
| 116 | + } |
| 117 | + return c.eltConverter.genTo(arg) |
| 118 | +} |
| 119 | + |
| 120 | +func (c sliceConverter) genTransformTo() string { |
| 121 | + panic("sliceConverter.genToSlice called") |
| 122 | +} |
| 123 | + |
| 124 | +func (c sliceConverter) genTransformFrom() string { |
| 125 | + panic("sliceConverter.genFromSlice called") |
| 126 | +} |
| 127 | + |
| 128 | +// Only the values are converted. |
| 129 | +type mapConverter struct { |
| 130 | + valueConverter converter |
| 131 | +} |
| 132 | + |
| 133 | +func (c mapConverter) genFrom(arg string) string { |
| 134 | + if fn := c.valueConverter.genTransformFrom(); fn != "" { |
| 135 | + return fmt.Sprintf("support.TransformMapValues(%s, %s)", arg, fn) |
| 136 | + } |
| 137 | + return c.valueConverter.genFrom(arg) |
| 138 | +} |
| 139 | + |
| 140 | +func (c mapConverter) genTo(arg string) string { |
| 141 | + if fn := c.valueConverter.genTransformTo(); fn != "" { |
| 142 | + return fmt.Sprintf("support.TransformMapValues(%s, %s)", arg, fn) |
| 143 | + } |
| 144 | + return c.valueConverter.genTo(arg) |
| 145 | +} |
| 146 | + |
| 147 | +func (c mapConverter) genTransformTo() string { |
| 148 | + panic("mapConverter.genToSlice called") |
| 149 | +} |
| 150 | + |
| 151 | +func (c mapConverter) genTransformFrom() string { |
| 152 | + panic("mapConverter.genFromSlice called") |
| 153 | +} |
0 commit comments