~whereswaldon/forest-go

effb84ecc2c5bacb32a038a7d0c87ba5713fe45c — Chris Waldon 2 months ago 972f2af
feat: rename validation methods for clarity and add helper

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
3 files changed, 37 insertions(+), 25 deletions(-)

M cmd/forest/main.go
M grove/grove_test.go
M nodes.go
M cmd/forest/main.go => cmd/forest/main.go +1 -1
@@ 312,7 312,7 @@ func showNode(args []string, commandName string, fromBytes func([]byte) (forest.
	if err != nil {
		return err
	}
	if err := c.ValidateShallow(); err != nil {
	if err := c.ValidateInternal(); err != nil {
		return err
	}
	text, err := json.Marshal(c)

M grove/grove_test.go => grove/grove_test.go +2 -2
@@ 580,11 580,11 @@ func (e errNode) CreatedAt() time.Time {
	return time.Now()
}

func (e errNode) ValidateDeep(forest.Store) error {
func (e errNode) ValidateReferences(forest.Store) error {
	return e.error
}

func (e errNode) ValidateShallow() error {
func (e errNode) ValidateInternal() error {
	return e.error
}


M nodes.go => nodes.go +34 -22
@@ 25,12 25,24 @@ type Node interface {
	ParentID() *fields.QualifiedHash
	TreeDepth() fields.TreeDepth
	TwigMetadata() (*twig.Data, error)
	ValidateDeep(Store) error
	ValidateShallow() error
	ValidateReferences(Store) error
	ValidateInternal() error
	encoding.BinaryMarshaler
	encoding.BinaryUnmarshaler
}

// ValidateNode calls ValidateInternal and ValidateReferences on a node,
// returning the first error encountered (or nil if there were no errors).
func ValidateNode(n Node, store Store) error {
	if err := n.ValidateInternal(); err != nil {
		return err
	}
	if err := n.ValidateReferences(store); err != nil {
		return err
	}
	return nil
}

// NodeTypeOf returns the NodeType of the provided binary-marshaled node.
// If the provided bytes are not a forest node or the type cannot be determined,
// an error will be returned and the first return value must be ignored.


@@ 142,10 154,10 @@ func (n *CommonNode) Equals(n2 *CommonNode) bool {
		n.Author.Equals(&n2.Author)
}

// ValidateShallow checks all fields for internal validity. It does not check
// ValidateInternal checks all fields for internal validity. It does not check
// the existence or validity of nodes referenced from this node. If the node
// validates, ValidateShallow returns `nil`.
func (n *CommonNode) ValidateShallow() error {
// validates, ValidateInternal returns `nil`.
func (n *CommonNode) ValidateInternal() error {
	if _, validType := fields.ValidNodeTypes[n.Type]; !validType {
		return fmt.Errorf("%d is not a valid node type", n.Type)
	}


@@ 168,8 180,8 @@ func (n *CommonNode) ValidateShallow() error {
	return nil
}

// ValidateDeep checks for the existence of all referenced nodes within the provided store.
func (n *CommonNode) ValidateDeep(store Store) error {
// ValidateReferences checks for the existence of all referenced nodes within the provided store.
func (n *CommonNode) ValidateReferences(store Store) error {
	// ensure known parent
	if !n.Parent.Equals(fields.NullHash()) {
		if _, has, err := store.Get(&n.Parent); !has {


@@ 273,10 285,10 @@ func (i *Identity) Equals(other interface{}) bool {
		i.Trailer.Equals(&i2.Trailer)
}

// ValidateShallow checks all fields for internal validity. It does not check
// ValidateInternal checks all fields for internal validity. It does not check
// the existence or validity of nodes referenced from this node.
func (i *Identity) ValidateShallow() error {
	if err := i.CommonNode.ValidateShallow(); err != nil {
func (i *Identity) ValidateInternal() error {
	if err := i.CommonNode.ValidateInternal(); err != nil {
		return err
	}
	needsValidation := []Validator{&i.Name, &i.PublicKey}


@@ 300,8 312,8 @@ func (i *Identity) ValidateShallow() error {
	return nil
}

// ValidateDeep checks all referenced nodes for existence within the store.
func (i *Identity) ValidateDeep(store Store) error {
// ValidateReferences checks all referenced nodes for existence within the store.
func (i *Identity) ValidateReferences(store Store) error {
	return nil
}



@@ 354,10 366,10 @@ func (c *Community) Equals(other interface{}) bool {
		c.Trailer.Equals(&c2.Trailer)
}

// ValidateShallow checks all fields for internal validity. It does not check
// ValidateInternal checks all fields for internal validity. It does not check
// the existence or validity of nodes referenced from this node.
func (c *Community) ValidateShallow() error {
	if err := c.CommonNode.ValidateShallow(); err != nil {
func (c *Community) ValidateInternal() error {
	if err := c.CommonNode.ValidateInternal(); err != nil {
		return err
	}
	needsValidation := []Validator{&c.Name}


@@ 381,8 393,8 @@ func (c *Community) ValidateShallow() error {
	return nil
}

// ValidateDeep checks all referenced nodes for existence within the store.
func (c *Community) ValidateDeep(store Store) error {
// ValidateReferences checks all referenced nodes for existence within the store.
func (c *Community) ValidateReferences(store Store) error {
	if _, has, err := store.Get(&c.Author); !has {
		return fmt.Errorf("Missing author node %v", c.Author)
	} else if err != nil {


@@ 442,10 454,10 @@ func (r *Reply) Equals(other interface{}) bool {
		r.Trailer.Equals(&r2.Trailer)
}

// ValidateShallow checks all fields for internal validity. It does not check
// ValidateInternal checks all fields for internal validity. It does not check
// the existence or validity of nodes referenced from this node.
func (r *Reply) ValidateShallow() error {
	if err := r.CommonNode.ValidateShallow(); err != nil {
func (r *Reply) ValidateInternal() error {
	if err := r.CommonNode.ValidateInternal(); err != nil {
		return err
	}
	needsValidation := []Validator{&r.Content, &r.CommunityID, &r.ConversationID}


@@ 473,8 485,8 @@ func (r *Reply) ValidateShallow() error {
	return nil
}

// ValidateDeep checks all referenced nodes for existence within the store.
func (r *Reply) ValidateDeep(store Store) error {
// ValidateReferences checks all referenced nodes for existence within the store.
func (r *Reply) ValidateReferences(store Store) error {
	needed := []*fields.QualifiedHash{&r.Author, &r.Parent, &r.CommunityID}
	if r.Depth > fields.TreeDepth(1) {
		needed = append(needed, &r.ConversationID)