this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

encoding/toml: add first implementation of an encoder

For now, all we test is that it can re-encode all the decoder test cases
because it does not yet aim to keep any comments, style, order,
or structure from the original CUE value.
The go-toml library we use is not able to encode its AST nodes to TOML,
so for now we encode as Go "any" values which drop that information.

This will be resolved in the near future via an encoder that produces
TOML syntax directly without going through an "any" phase first.
For the time being, this basic encoder works,
even if it produces unnecessarily verbose output such as

[foo]
[foo.bar]
[foo.bar.'baz.zzz zzz']
one = '1'

rather than the shorter

[foo.bar.'baz.zzz zzz']
one = "1"

Updates #68.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I5012bba77fb77b8bf1dd1d73615d34c3438cd147
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198539
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+64
+17
encoding/toml/decode_test.go
··· 764 764 qt.Assert(t, qt.IsNil(err)) 765 765 t.Logf("json.Marshal via CUE:\t%s\n", jsonCUE) 766 766 qt.Assert(t, qt.JSONEquals(jsonCUE, unmarshalTOML)) 767 + 768 + // Ensure that the decoded CUE can be re-encoded as TOML, 769 + // and the resulting TOML is still JSON-equivalent. 770 + t.Run("reencode", func(t *testing.T) { 771 + sb := new(strings.Builder) 772 + enc := toml.NewEncoder(sb) 773 + 774 + err := enc.Encode(val) 775 + qt.Assert(t, qt.IsNil(err)) 776 + cueTOML := sb.String() 777 + t.Logf("reencoded TOML:\n%s", cueTOML) 778 + 779 + var unmarshalCueTOML any 780 + err = gotoml.Unmarshal([]byte(cueTOML), &unmarshalCueTOML) 781 + qt.Assert(t, qt.IsNil(err)) 782 + qt.Assert(t, qt.JSONEquals(jsonCUE, unmarshalCueTOML)) 783 + }) 767 784 }) 768 785 } 769 786 }
+47
encoding/toml/encode.go
··· 1 + // Copyright 2024 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 toml 16 + 17 + import ( 18 + "io" 19 + 20 + "github.com/pelletier/go-toml/v2" 21 + 22 + "cuelang.org/go/cue" 23 + ) 24 + 25 + // TODO(mvdan): encode options 26 + 27 + // TODO(mvdan): the encoder below is based on map[string]any since go-toml/v2/unstable 28 + // does not support printing or encoding Nodes; this means no support for comments, 29 + // positions such as empty lines, or the relative order of fields. 30 + 31 + // NewEncoder creates an encoder to stream encoded TOML bytes. 32 + func NewEncoder(w io.Writer) *Encoder { 33 + return &Encoder{encoder: toml.NewEncoder(w)} 34 + } 35 + 36 + // Encoder implements the encoding state. 37 + type Encoder struct { 38 + encoder *toml.Encoder 39 + } 40 + 41 + func (e *Encoder) Encode(val cue.Value) error { 42 + var v any 43 + if err := val.Decode(&v); err != nil { 44 + return err 45 + } 46 + return e.encoder.Encode(v) 47 + }