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 package main
import ( import (
"encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"runtime"
"runtime/pprof"
"github.com/alcionai/corso/src/cmd/jsondebug/common" "github.com/alcionai/corso/src/cmd/jsondebug/common"
"github.com/buger/jsonparser" "github.com/buger/jsonparser"
) )
func main() { func main() {
f, err := os.Open(common.FileName) defer func() {
if err != nil { common.PrintMemUsage()
fmt.Printf("Error opening input file: %v\n", err) f, err := os.Create("mem.prof")
return 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 { if err != nil {
fmt.Printf("Error reading file: %v\n", err) fmt.Printf("Error reading file: %v\n", err)
return return
} }
common.PrintMemUsage()
output := common.FooArray{ output := common.FooArray{
Entries: []*common.Foo{}, Entries: []*common.Foo{},
} }
_ = output _ = output
common.PrintMemUsage()
// var handler func([]byte, []byte, jsonparser.ValueType, int) error // var handler func([]byte, []byte, jsonparser.ValueType, int) error
// handler := func(key []byte, value []byte, dataType jsonparser.ValueType, offset 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) // 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) { jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
id, _ := jsonparser.GetString(value, "id") e, errInner := getEntry(value)
content, _, _, errInner := jsonparser.Get(value, "data")
if errInner != nil { if errInner != nil {
fmt.Printf("Error decoding input: %v\n", errInner) fmt.Printf("Error decoding input2: %v\n", errInner)
return return
} }
cpBuf := make([]byte, len(content))
_ = copy(cpBuf, content)
e := &common.Foo{
ID: id,
Content: cpBuf,
}
output.Entries = append(output.Entries, e) output.Entries = append(output.Entries, e)
}, "entries") }, "entries")
common.PrintMemUsage() common.PrintMemUsage()
// for _, e := range output.Entries { fmt.Printf("Decoded %d entries\n", len(output.Entries))
// fmt.Printf("ID: '%s'\n Content: %s \n", e.ID, e.Content) }
// }
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
} }