Marcel van Lohuizen | d34647b | 2018-12-10 15:38:51 +0100 | [diff] [blame^] | 1 | // Copyright 2018 The 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 encoding provides support for managing data format files supported |
| 16 | // by CUE. |
| 17 | package encoding // import "cuelang.org/go/cue/encoding" |
| 18 | |
| 19 | import "strings" |
| 20 | |
| 21 | // Encoding represents a data encoding. |
| 22 | type Encoding struct { |
| 23 | name string |
| 24 | } |
| 25 | |
| 26 | // Name returns a lowercase name of an encoding. This is conventionally the most |
| 27 | // common file extension in lower case. |
| 28 | func (e *Encoding) Name() string { |
| 29 | return e.name |
| 30 | } |
| 31 | |
| 32 | // All returns all known encodings. |
| 33 | func All() []*Encoding { |
| 34 | return []*Encoding{jsonEnc, yamlEnc} |
| 35 | } |
| 36 | |
| 37 | // MapExtension returns the likely encoding for a given file extension. |
| 38 | func MapExtension(ext string) *Encoding { |
| 39 | return extensions[strings.ToLower(ext)] |
| 40 | } |
| 41 | |
| 42 | var ( |
| 43 | jsonEnc = &Encoding{name: "json"} |
| 44 | yamlEnc = &Encoding{name: "yaml"} |
| 45 | ) |
| 46 | |
| 47 | // extensions maps a file extension to a Kind. |
| 48 | var extensions = map[string]*Encoding{ |
| 49 | ".json": jsonEnc, |
| 50 | ".jsonl": jsonEnc, |
| 51 | ".ndjson": jsonEnc, |
| 52 | ".yaml": yamlEnc, |
| 53 | ".yml": yamlEnc, |
| 54 | } |