Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 1 | // Copyright 2019 CUE Authors |
| 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 protobuf |
| 16 | |
Marcel van Lohuizen | fbcb339 | 2019-06-25 11:02:21 +0200 | [diff] [blame] | 17 | import ( |
| 18 | "text/scanner" |
| 19 | |
| 20 | "github.com/emicklei/proto" |
| 21 | ) |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 22 | |
| 23 | func protoToCUE(typ string, options []*proto.Option) (ref string, ok bool) { |
| 24 | t, ok := scalars[typ] |
| 25 | return t, ok |
| 26 | } |
| 27 | |
| 28 | var scalars = map[string]string{ |
| 29 | // Differing |
| 30 | "sint32": "int32", |
| 31 | "sint64": "int64", |
| 32 | "fixed32": "uint32", |
| 33 | "fixed64": "uint64", |
| 34 | "sfixed32": "int32", |
| 35 | "sfixed64": "int64", |
| 36 | |
| 37 | // Identical to CUE |
| 38 | "int32": "int32", |
| 39 | "int64": "int64", |
| 40 | "uint32": "uint32", |
| 41 | "uint64": "uint64", |
| 42 | |
| 43 | "double": "float64", |
| 44 | "float": "float32", |
| 45 | |
| 46 | "bool": "bool", |
| 47 | "string": "string", |
| 48 | "bytes": "bytes", |
| 49 | } |
| 50 | |
| 51 | var timePkg = &protoConverter{ |
Marcel van Lohuizen | 93e9597 | 2019-06-27 16:47:52 +0200 | [diff] [blame] | 52 | id: "time", |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 53 | goPkg: "time", |
| 54 | goPkgPath: "time", |
| 55 | } |
| 56 | |
| 57 | func (p *protoConverter) setBuiltin(from, to string, pkg *protoConverter) { |
Marcel van Lohuizen | 93e9597 | 2019-06-27 16:47:52 +0200 | [diff] [blame] | 58 | p.scope[0][from] = mapping{to, "", pkg} |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Marcel van Lohuizen | 9fe5fed | 2019-06-25 13:04:40 +0200 | [diff] [blame] | 61 | func (p *protoConverter) mapBuiltinPackage(pos scanner.Position, file string, required bool) { |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 62 | // Map some builtin types to their JSON/CUE mappings. |
| 63 | switch file { |
| 64 | case "gogoproto/gogo.proto": |
| 65 | |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 66 | default: |
Marcel van Lohuizen | 9fe5fed | 2019-06-25 13:04:40 +0200 | [diff] [blame] | 67 | if required { |
| 68 | failf(pos, "import %q not found", file) |
| 69 | } |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 70 | } |
| 71 | } |