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
import (
"encoding/json"
"fmt"
"runtime"
"time"
)
const (
@ -16,7 +18,16 @@ type FooArray 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() {

View File

@ -5,8 +5,10 @@ import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/alcionai/corso/src/cmd/jsondebug/common"
"github.com/google/uuid"
)
func main() {
@ -28,8 +30,18 @@ func main() {
)
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)
}

View File

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