hclpack: Start of definitions for JSON marshaling/unmarshaling
This is not yet complete.
This commit is contained in:
parent
5e07d8e1f9
commit
227ba9e86b
54
hclpack/json_marshal.go
Normal file
54
hclpack/json_marshal.go
Normal file
@ -0,0 +1,54 @@
|
||||
package hclpack
|
||||
|
||||
// MarshalJSON is an implementation of Marshaler from encoding/json, allowing
|
||||
// bodies to be included in other types that are JSON-marshalable.
|
||||
//
|
||||
// The result of MarshalJSON is optimized for compactness rather than easy
|
||||
// human consumption/editing. Use UnmarshalJSON to decode it.
|
||||
func (b *Body) MarshalJSON() ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON is an implementation of Unmarshaler from encoding/json,
|
||||
// allowing bodies to be included in other types that are JSON-unmarshalable.
|
||||
func (b *Body) UnmarshalJSON([]byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type jsonHeader struct {
|
||||
Body bodyJSON `json:"b"`
|
||||
|
||||
Sources []string `json:"s,omitempty"`
|
||||
Pos positionsPacked `json:"p,omitempty"`
|
||||
}
|
||||
|
||||
type bodyJSON struct {
|
||||
// Files are the source filenames that were involved in
|
||||
Attrs map[string]attrJSON `json:"a,omitempty"`
|
||||
Blocks []blockJSON `json:"b,omitempty"`
|
||||
MissingItemRange rangePacked `json:"r,omitempty"`
|
||||
}
|
||||
|
||||
type attrJSON struct {
|
||||
Expr exprJSON `json:"e"`
|
||||
|
||||
// Ranges contains the full range followed by the name range
|
||||
Ranges rangesPacked `json:"r,omitempty"`
|
||||
}
|
||||
|
||||
type blockJSON struct {
|
||||
Type string `json:"t"`
|
||||
Labels []string `json:"l,omitempty"`
|
||||
Body bodyJSON `json:"b,omitempty"`
|
||||
|
||||
// Ranges contains the DefRange followed by the TypeRange
|
||||
Ranges rangesPacked `json:"r,omitempty"`
|
||||
}
|
||||
|
||||
type exprJSON struct {
|
||||
Source string `json:"s"`
|
||||
Syntax string `json:"t"`
|
||||
|
||||
// Ranges contains the Range followed by the StartRange
|
||||
Ranges rangesPacked `json:"r,omitempty"`
|
||||
}
|
51
hclpack/positions_packed.go
Normal file
51
hclpack/positions_packed.go
Normal file
@ -0,0 +1,51 @@
|
||||
package hclpack
|
||||
|
||||
// positionsPacked is a delta-based representation of source positions
|
||||
// that implements encoding.TextMarshaler and encoding.TextUnmarshaler using
|
||||
// a compact variable-length quantity encoding to mimimize the overhead of
|
||||
// storing source positions.
|
||||
//
|
||||
// Serializations of the other types in this package can refer to positions
|
||||
// in a positionsPacked by index.
|
||||
type positionsPacked []positionPacked
|
||||
|
||||
type positionPacked struct {
|
||||
FileIdx int
|
||||
LineDelta, ColumnDelta, ByteDelta int
|
||||
}
|
||||
|
||||
func (pp positionsPacked) Len() int {
|
||||
return len(pp)
|
||||
}
|
||||
|
||||
func (pp positionsPacked) Less(i, j int) bool {
|
||||
return pp[i].FileIdx < pp[j].FileIdx
|
||||
}
|
||||
|
||||
func (pp positionsPacked) Swap(i, j int) {
|
||||
pp[i], pp[j] = pp[j], pp[i]
|
||||
}
|
||||
|
||||
// posOfs is an index into a positionsPacked. The zero value of this type
|
||||
// represents the absense of a position.
|
||||
type posOfs int
|
||||
|
||||
func newPosOffs(idx int) posOfs {
|
||||
return posOfs(idx + 1)
|
||||
}
|
||||
|
||||
func (o posOfs) Index() int {
|
||||
return int(o - 1)
|
||||
}
|
||||
|
||||
// rangePacked is a range represented as two indexes into a positionsPacked.
|
||||
// This implements encoding.TextMarshaler and encoding.TextUnmarshaler using
|
||||
// a compact variable-length quantity encoding.
|
||||
type rangePacked struct {
|
||||
Start posOfs
|
||||
End posOfs
|
||||
}
|
||||
|
||||
// rangesPacked represents a sequence of ranges, packed compactly into a single
|
||||
// string during marshaling.
|
||||
type rangesPacked []rangePacked
|
Loading…
Reference in New Issue
Block a user