blob: c6c85489b4f3a2d817ea2ad4243120bb41b2d611 [file] [log] [blame]
Marcel van Lohuizen5274e982019-04-28 17:51:43 +02001// 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
15package protobuf
16
Marcel van Lohuizenfbcb3392019-06-25 11:02:21 +020017import (
18 "text/scanner"
19
20 "github.com/emicklei/proto"
21)
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020022
23func protoToCUE(typ string, options []*proto.Option) (ref string, ok bool) {
24 t, ok := scalars[typ]
25 return t, ok
26}
27
28var 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
51var timePkg = &protoConverter{
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020052 id: "time",
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020053 goPkg: "time",
54 goPkgPath: "time",
55}
56
57func (p *protoConverter) setBuiltin(from, to string, pkg *protoConverter) {
Marcel van Lohuizen93e95972019-06-27 16:47:52 +020058 p.scope[0][from] = mapping{to, "", pkg}
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020059}
60
Marcel van Lohuizen9fe5fed2019-06-25 13:04:40 +020061func (p *protoConverter) mapBuiltinPackage(pos scanner.Position, file string, required bool) {
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020062 // Map some builtin types to their JSON/CUE mappings.
63 switch file {
64 case "gogoproto/gogo.proto":
65
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020066 default:
Marcel van Lohuizen9fe5fed2019-06-25 13:04:40 +020067 if required {
68 failf(pos, "import %q not found", file)
69 }
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020070 }
71}