Change struct to include RawMessage and update jsonparser

This commit is contained in:
Vaibhav Kamra 2023-03-30 20:47:43 -07:00
parent dddbd36969
commit 62bfed94d6
3 changed files with 45 additions and 8 deletions

View File

@ -1,8 +1,10 @@
package common package common
import ( import (
"encoding/json"
"fmt" "fmt"
"runtime" "runtime"
"time"
) )
const ( const (
@ -16,7 +18,16 @@ type FooArray struct {
} }
type Foo struct { type Foo struct {
A []byte ID string `json:"id"`
Labels map[string]string `json:"labels"`
ModTime time.Time `json:"modified"`
Deleted bool `json:"deleted,omitempty"`
Content json.RawMessage `json:"data"`
}
type Content struct {
ID string `json:"id"`
Data []byte `json:"data"`
} }
func PrintMemUsage() { func PrintMemUsage() {

View File

@ -5,8 +5,10 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"time"
"github.com/alcionai/corso/src/cmd/jsondebug/common" "github.com/alcionai/corso/src/cmd/jsondebug/common"
"github.com/google/uuid"
) )
func main() { func main() {
@ -28,8 +30,18 @@ func main() {
) )
return return
} }
content := common.Content{
ID: uuid.NewString(),
Data: buf,
}
payload, _ := json.Marshal(content)
item := common.Foo{A: buf} item := common.Foo{
ID: uuid.NewString(),
Labels: map[string]string{"foo": "bar"},
ModTime: time.Now(),
Content: payload,
}
data.Entries = append(data.Entries, &item) data.Entries = append(data.Entries, &item)
} }

View File

@ -32,20 +32,34 @@ func main() {
common.PrintMemUsage() 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)
// return nil
// }
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) {
buf, _, _, errInner := jsonparser.Get(value, "A")
id, _ := jsonparser.GetString(value, "id")
content, _, _, errInner := jsonparser.Get(value, "data")
if errInner != nil { if errInner != nil {
fmt.Printf("Error decoding input: %v\n", err) fmt.Printf("Error decoding input: %v\n", errInner)
return return
} }
_ = value
cpBuf := make([]byte, len(buf)) cpBuf := make([]byte, len(content))
_ = copy(cpBuf, buf) _ = copy(cpBuf, content)
e := &common.Foo{ e := &common.Foo{
A: cpBuf, 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("ID: '%s'\n Content: %s \n", e.ID, e.Content)
// }
} }