Skip to content

Commit

Permalink
add docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
bign8 committed May 22, 2021
1 parent 68637d7 commit f3ac688
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions unpacker/unpacker.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package unpacker implements the logic described at https://www.unpacker.uk/ to unpack compressed JSON objects.
package unpacker

import (
Expand All @@ -8,11 +9,17 @@ import (
"strings"
)

// Unpakcker holds a set of Transforms and Substitutions needed to unpack compressed JSON.
// This structure should allow the the Transform and Substitution object to only need to be parsed once,
// while allowing multiple unpackings using the same configuration.
type Unpacker struct {
Transforms map[string]Transform
Subs Substitution
}

// A Transform holds information on how to modify various JSON objects.
//
// docs: https://www.unpacker.uk/specification#transformation-object
type Transform struct {
Assign []string `json:"assignKeys,omitempty"`
Items string `json:"arrayItems,omitempty"`
Expand All @@ -30,6 +37,7 @@ func (t Transform) String() string {
return string(x)
}

// internal for now but could be exposed, makes it so we can parse nested transforms without infinite recursive loops
type trans Transform

func (t *trans) UnmarshalJSON(bits []byte) error {
Expand Down Expand Up @@ -67,6 +75,9 @@ func (t trans) MarshalJSON() ([]byte, error) {
return bits, nil
}

// A Substitution object provides a method of removing repeditive data by having shortened keys.
//
// docs: https://www.unpacker.uk/specification#substitution-object
type Substitution map[string]interface{}

// ParseTransforms parses a set of json tranforms.
Expand Down Expand Up @@ -100,6 +111,10 @@ func (u *Unpacker) AddTransform(key string, trans Transform) {
u.Transforms[key] = trans
}

// Unpack expands JSON using the transforms and substitutions defined on the unpacker object.
// Additionally, a viariadic index in the source data is also used to reduce size.
//
// docs: https://www.unpacker.uk/specification
func (u Unpacker) Unpack(source []byte) ([]byte, error) {
state := &unpackState{mem: make(map[string]interface{})}
state.trans = append(state.trans, u.Transforms)
Expand Down
2 changes: 1 addition & 1 deletion unpacker/unpacker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ func (test UnpackerTest) out() []byte {
if err != nil {
panic(err)
}
bits, err := json.MarshalIndent(obj, "", " ")
bits, err := json.Marshal(obj)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit f3ac688

Please sign in to comment.