···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Package encoding provides support for managing data format files supported
1616+// by CUE.
1717+package encoding // import "cuelang.org/go/cue/encoding"
1818+1919+import "strings"
2020+2121+// Encoding represents a data encoding.
2222+type Encoding struct {
2323+ name string
2424+}
2525+2626+// Name returns a lowercase name of an encoding. This is conventionally the most
2727+// common file extension in lower case.
2828+func (e *Encoding) Name() string {
2929+ return e.name
3030+}
3131+3232+// All returns all known encodings.
3333+func All() []*Encoding {
3434+ return []*Encoding{jsonEnc, yamlEnc}
3535+}
3636+3737+// MapExtension returns the likely encoding for a given file extension.
3838+func MapExtension(ext string) *Encoding {
3939+ return extensions[strings.ToLower(ext)]
4040+}
4141+4242+var (
4343+ jsonEnc = &Encoding{name: "json"}
4444+ yamlEnc = &Encoding{name: "yaml"}
4545+)
4646+4747+// extensions maps a file extension to a Kind.
4848+var extensions = map[string]*Encoding{
4949+ ".json": jsonEnc,
5050+ ".jsonl": jsonEnc,
5151+ ".ndjson": jsonEnc,
5252+ ".yaml": yamlEnc,
5353+ ".yml": yamlEnc,
5454+}