You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: jose_encode.go
    path of file: ./repos/sealer/vendor/github.com/ceramicnetwork/go-dag-jose/dagjose
    
    the code of the file until where you have to start completion: package dagjose

import (
	"errors"
	"io"

	"github.com/ipfs/go-cid"
	"github.com/ipld/go-ipld-prime/codec"
	"github.com/ipld/go-ipld-prime/codec/dagcbor"
	"github.com/ipld/go-ipld-prime/datamodel"
	"github.com/ipld/go-ipld-prime/linking/cid"
	"github.com/ipld/go-ipld-prime/schema"
	"github.com/multiformats/go-multibase"
)

// Encode walks the given datamodel.Node and serializes it to the given io.Writer. Encode fits the codec.Encoder
// function interface.
func Encode(n datamodel.Node, w io.Writer) error {
	// "flattened" fields are not included in the schema and thus never encoded. That means that this cannot have been
	// called on a JOSE-related node because we wouldn't have gotten this far without an error have occurred earlier.
	// We'll assume this is some sort of Map-type node that we can reconstruct to be in a
    
func Encode(n datamodel.Node, w io.Writer) error {
	// "flattened" fields are not included in the schema and thus never encoded. That means that this cannot have been
	// called on a JOSE-related node because we wouldn't have gotten this far without an error have occurred earlier.
	// We'll assume this is some sort of Map-type node that we can reconstruct to be in a "general" form before any
	// actual JOSE-related operations are performed on it.
	if jwe, err := isJWE(n); err != nil {
		return err
	} else if jwe {
		if n, err := unflattenJWE(n); err != nil {
			return err
		} else if err := EncodeJWE(n, w); err != nil {
			return err
		}
	} else if jws, err := isJWS(n); err != nil {
		return err
	} else if jws {
		if n, err := unflattenJWS(n); err != nil {
			return err
		} else if err := EncodeJWS(n, w); err != nil {
			return err
		}
	} else {
		return errors.New("invalid JOSE object")
	}
	return nil
}