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 defines functionality for parsing protocol buffer |
| 16 | // definitions and instances. |
| 17 | // |
| 18 | // TODO: this package can become public once we have found a good nest for it. |
| 19 | package protobuf |
| 20 | |
| 21 | import ( |
| 22 | "fmt" |
| 23 | "io" |
| 24 | |
| 25 | "cuelang.org/go/cue/ast" |
| 26 | ) |
| 27 | |
| 28 | // Config specifies the environment into which to parse a proto definition file. |
| 29 | type Config struct { |
| 30 | Paths []string |
| 31 | } |
| 32 | |
| 33 | // Parse parses a single proto file and returns its contents translated to |
| 34 | // a CUE file. Imports are resolved using the path define in Config. |
| 35 | // If body is not nil, it will use this as the contents of the file. Otherwise |
| 36 | // Parse will open the given file name at the fully qualified path. |
| 37 | // |
| 38 | // The following field options are supported: |
| 39 | // (cue.val) string CUE constraint for this field. The string may |
| 40 | // refer to other fields in a message definition. |
| 41 | // (cue.opt) FieldOptions |
| 42 | // required bool Defines the field is required. Use with |
| 43 | // caution. |
| 44 | func Parse(filename string, body io.Reader, c *Config) (f *ast.File, err error) { |
| 45 | state := &sharedState{ |
| 46 | paths: c.Paths, |
| 47 | } |
| 48 | p, err := state.parse(filename, body) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | return p.file, nil |
| 53 | } |
| 54 | |
Marcel van Lohuizen | 58e93da | 2019-05-14 16:31:19 +0200 | [diff] [blame] | 55 | // Error describes the location and cause of an error. |
| 56 | type Error struct { |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 57 | Filename string |
| 58 | Path string |
| 59 | Err error |
| 60 | } |
| 61 | |
Marcel van Lohuizen | 58e93da | 2019-05-14 16:31:19 +0200 | [diff] [blame] | 62 | func (p *Error) Unwrap() error { return p.Err } |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 63 | |
Marcel van Lohuizen | 58e93da | 2019-05-14 16:31:19 +0200 | [diff] [blame] | 64 | func (p *Error) Error() string { |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 65 | if p.Path == "" { |
| 66 | return fmt.Sprintf("parse of file %q failed: %v", p.Filename, p.Err) |
| 67 | } |
| 68 | return fmt.Sprintf("parse of file %q failed at %s: %v", p.Filename, p.Path, p.Err) |
| 69 | } |
| 70 | |
| 71 | // TODO |
| 72 | // func GenDefinition |
| 73 | |
| 74 | // func MarshalText(cue.Value) (string, error) { |
| 75 | // return "", nil |
| 76 | // } |
| 77 | |
| 78 | // func MarshalBytes(cue.Value) ([]byte, error) { |
| 79 | // return nil, nil |
| 80 | // } |
| 81 | |
| 82 | // func UnmarshalText(descriptor cue.Value, b string) (ast.Expr, error) { |
| 83 | // return nil, nil |
| 84 | // } |
| 85 | |
| 86 | // func UnmarshalBytes(descriptor cue.Value, b []byte) (ast.Expr, error) { |
| 87 | // return nil, nil |
| 88 | // } |