mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-09 09:35:00 +09:00
GO-2030 merge
This commit is contained in:
commit
77a582ebb0
46 changed files with 1339 additions and 477 deletions
67
tests/blockbuilder/block_render.go
Normal file
67
tests/blockbuilder/block_render.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package blockbuilder
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogo/protobuf/jsonpb"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
|
||||
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
|
||||
)
|
||||
|
||||
type blockView struct {
|
||||
Id string
|
||||
Fields *json.RawMessage `json:"Fields,omitempty"`
|
||||
Children []*Block `json:"Children,omitempty"`
|
||||
Restrictions *model.BlockRestrictions `json:"Restrictions,omitempty"`
|
||||
BackgroundColor string `json:"BackgroundColor,omitempty"`
|
||||
Align model.BlockAlign `json:"Align,omitempty"`
|
||||
VerticalAlign model.BlockVerticalAlign `json:"VerticalAlign,omitempty"`
|
||||
Content *json.RawMessage `json:"Content"`
|
||||
}
|
||||
|
||||
func marshalProtoMessage(pbMessage proto.Message) (*json.RawMessage, error) {
|
||||
marshaller := &jsonpb.Marshaler{}
|
||||
raw, err := marshaller.MarshalToString(pbMessage)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal content: %w", err)
|
||||
}
|
||||
rawMessage := json.RawMessage(raw)
|
||||
return &rawMessage, nil
|
||||
}
|
||||
|
||||
func (b *Block) MarshalJSON() ([]byte, error) {
|
||||
var (
|
||||
err error
|
||||
rawContent *json.RawMessage
|
||||
)
|
||||
if content := b.block.Content; content != nil {
|
||||
contentWrapper := &model.Block{
|
||||
Content: content,
|
||||
}
|
||||
rawContent, err = marshalProtoMessage(contentWrapper)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal content: %w", err)
|
||||
}
|
||||
}
|
||||
var rawFields *json.RawMessage
|
||||
if fields := b.block.Fields; fields != nil {
|
||||
rawFields, err = marshalProtoMessage(b.block.Fields)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal fields: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
v := blockView{
|
||||
Id: b.block.Id,
|
||||
Fields: rawFields,
|
||||
Children: b.children,
|
||||
Restrictions: b.block.Restrictions,
|
||||
BackgroundColor: b.block.BackgroundColor,
|
||||
Align: b.block.Align,
|
||||
VerticalAlign: b.block.VerticalAlign,
|
||||
Content: rawContent,
|
||||
}
|
||||
return json.Marshal(v)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue