blob: c5f2ca6b38bf51a1555a39c1533d67debdad3d24 [file] [log] [blame]
Marcel van Lohuizend34647b2018-12-10 15:38:51 +01001// 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.
17package encoding // import "cuelang.org/go/cue/encoding"
18
19import "strings"
20
21// Encoding represents a data encoding.
22type 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.
28func (e *Encoding) Name() string {
29 return e.name
30}
31
32// All returns all known encodings.
33func All() []*Encoding {
34 return []*Encoding{jsonEnc, yamlEnc}
35}
36
37// MapExtension returns the likely encoding for a given file extension.
38func MapExtension(ext string) *Encoding {
39 return extensions[strings.ToLower(ext)]
40}
41
42var (
43 jsonEnc = &Encoding{name: "json"}
44 yamlEnc = &Encoding{name: "yaml"}
45)
46
47// extensions maps a file extension to a Kind.
48var extensions = map[string]*Encoding{
49 ".json": jsonEnc,
50 ".jsonl": jsonEnc,
51 ".ndjson": jsonEnc,
52 ".yaml": yamlEnc,
53 ".yml": yamlEnc,
54}