Handle all manifest entry fields

This commit is contained in:
Vaibhav Kamra 2023-03-31 00:34:28 -07:00
parent 62bfed94d6
commit 7f91344fda

View File

@ -1,37 +1,50 @@
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"runtime"
"runtime/pprof"
"github.com/alcionai/corso/src/cmd/jsondebug/common"
"github.com/buger/jsonparser"
)
func main() {
f, err := os.Open(common.FileName)
if err != nil {
fmt.Printf("Error opening input file: %v\n", err)
return
}
defer func() {
common.PrintMemUsage()
f, err := os.Create("mem.prof")
if err != nil {
fmt.Print("could not create memory profile: ", err)
return
}
defer f.Close() // error handling omitted for example
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
fmt.Print("could not write memory profile: ", err)
return
}
}()
defer f.Close()
common.PrintMemUsage()
data, err := ioutil.ReadAll(f)
data, err := ioutil.ReadFile(common.FileName)
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
return
}
common.PrintMemUsage()
output := common.FooArray{
Entries: []*common.Foo{},
}
_ = output
common.PrintMemUsage()
// var handler func([]byte, []byte, jsonparser.ValueType, int) error
// handler := func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
// fmt.Printf("Key: '%s'\n Value: '%s'\n Type: %s\n", string(key), string(value), dataType)
@ -40,26 +53,52 @@ func main() {
jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
id, _ := jsonparser.GetString(value, "id")
content, _, _, errInner := jsonparser.Get(value, "data")
e, errInner := getEntry(value)
if errInner != nil {
fmt.Printf("Error decoding input: %v\n", errInner)
fmt.Printf("Error decoding input2: %v\n", errInner)
return
}
cpBuf := make([]byte, len(content))
_ = copy(cpBuf, content)
e := &common.Foo{
ID: id,
Content: cpBuf,
}
output.Entries = append(output.Entries, e)
}, "entries")
common.PrintMemUsage()
// for _, e := range output.Entries {
// fmt.Printf("ID: '%s'\n Content: %s \n", e.ID, e.Content)
// }
fmt.Printf("Decoded %d entries\n", len(output.Entries))
}
func getEntry(data []byte) (*common.Foo, error) {
e := &common.Foo{}
jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
switch string(key) {
case "id":
e.ID = string(value)
case "labels":
err := json.Unmarshal(value, &e.Labels)
if err != nil {
return fmt.Errorf("unmarshalling labels: %w", err)
}
case "modified":
err := json.Unmarshal(value, &e.ModTime)
if err != nil {
return fmt.Errorf("unmarshalling modtime: %w", err)
}
case "deleted":
err := json.Unmarshal(value, &e.Deleted)
if err != nil {
return fmt.Errorf("unmarshalling deleted: %w", err)
}
case "data":
cpBuf := make([]byte, len(value))
_ = copy(cpBuf, value)
e.Content = cpBuf
default:
fmt.Printf("Unexpected Input: %v\n", key)
return errors.New("Unexpected Input: " + string(key))
}
return nil
})
return e, nil
}