Debian Codegraph @

json/json.go

raw symbols SCIP
1 // Copyright 2019 Red Hat, Inc
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 json
16
17 import (
18 "github.com/coreos/vcontext/tree"
19 // todo: rewrite this dep
20 json "github.com/coreos/go-json"
21 )
22
23 func UnmarshalToContext(raw []byte) (tree.Node, error) {
24 var ast json.Node
25 if err := json.Unmarshal(raw, &ast); err != nil {
26 return nil, err
27 }
28 node := fromJsonNode(ast)
29 tree.FixLineColumn(node, raw)
30 return node, nil
31 }
32
33 func fromJsonNode(n json.Node) tree.Node {
34 m := tree.MarkerFromIndices(int64(n.Start), int64(n.End))
35
36 switch v := n.Value.(type) {
37 case map[string]json.Node:
38 ret := tree.MapNode{
39 Marker: m,
40 Children: make(map[string]tree.Node, len(v)),
41 Keys: make(map[string]tree.Leaf, len(v)),
42 }
43 for key, child := range v {
44 ret.Children[key] = fromJsonNode(child)
45 ret.Keys[key] = tree.Leaf{
46 Marker: tree.MarkerFromIndices(int64(child.KeyStart), int64(child.KeyEnd)),
47 }
48 }
49 return ret
50 case []json.Node:
51 ret := tree.SliceNode{
52 Marker: m,
53 Children: make([]tree.Node, 0, len(v)),
54 }
55 for _, child := range v {
56 ret.Children = append(ret.Children, fromJsonNode(child))
57 }
58 return ret
59 default:
60 return tree.Leaf{
61 Marker: m,
62 }
63 }
64 }