// Code generated by cmd/lexgen (see Makefile's lexgen); DO NOT EDIT.

package org_tangled

// schema: org.tangled.repo.manifest

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"

	"github.com/bluesky-social/indigo/lex/util"
	cbg "github.com/whyrusleeping/cbor-gen"
)

const (
	RepoManifestNSID = "org.tangled.repo.manifest"
)

func init() {
	util.RegisterType("org.tangled.repo.manifest", &RepoManifest{})
} //
// RECORDTYPE: RepoManifest
type RepoManifest struct {
	LexiconTypeID string `json:"$type,const=org.tangled.repo.manifest" cborgen:"$type,const=org.tangled.repo.manifest"`
	// contributionPolicy: Who may open tickets and patches on this repo. anyone = any DID, members = the knot's member roll, collaborators = the owner plus repo collaborators. Absent means the knot-wide default.
	ContributionPolicy *string                   `json:"contributionPolicy,omitempty" cborgen:"contributionPolicy,omitempty"`
	CreatedAt          string                    `json:"createdAt" cborgen:"createdAt"`
	Declaration        *RepoManifest_Declaration `json:"declaration" cborgen:"declaration"`
	Description        *string                   `json:"description,omitempty" cborgen:"description,omitempty"`
	// source: source of the repo
	Source  *RepoManifest_Source `json:"source,omitempty" cborgen:"source,omitempty"`
	Spindle *string              `json:"spindle,omitempty" cborgen:"spindle,omitempty"`
	// topics: Topics related to the repo
	Topics []string `json:"topics,omitempty" cborgen:"topics,omitempty"`
	// website: Any URI related to the repo
	Website *string `json:"website,omitempty" cborgen:"website,omitempty"`
}

// RepoManifest_Declaration is a "declaration" in the org.tangled.repo.manifest schema.
type RepoManifest_Declaration struct {
	// name: repository name; the slug (rkey of org.tangled.repo.declaration) is lowercase(name)
	Name  string `json:"name" cborgen:"name"`
	Owner string `json:"owner" cborgen:"owner"`
}

// source of the repo
type RepoManifest_Source struct {
	RepoDefs_SourceTangled  *RepoDefs_SourceTangled
	RepoDefs_SourceExternal *RepoDefs_SourceExternal
}

func (t *RepoManifest_Source) MarshalJSON() ([]byte, error) {
	if t.RepoDefs_SourceTangled != nil {
		t.RepoDefs_SourceTangled.LexiconTypeID = "org.tangled.repo.defs#sourceTangled"
		return json.Marshal(t.RepoDefs_SourceTangled)
	}
	if t.RepoDefs_SourceExternal != nil {
		t.RepoDefs_SourceExternal.LexiconTypeID = "org.tangled.repo.defs#sourceExternal"
		return json.Marshal(t.RepoDefs_SourceExternal)
	}
	return nil, fmt.Errorf("cannot marshal empty enum")
}
func (t *RepoManifest_Source) UnmarshalJSON(b []byte) error {
	typ, err := util.TypeExtract(b)
	if err != nil {
		return err
	}

	switch typ {
	case "org.tangled.repo.defs#sourceTangled":
		t.RepoDefs_SourceTangled = new(RepoDefs_SourceTangled)
		return json.Unmarshal(b, t.RepoDefs_SourceTangled)
	case "org.tangled.repo.defs#sourceExternal":
		t.RepoDefs_SourceExternal = new(RepoDefs_SourceExternal)
		return json.Unmarshal(b, t.RepoDefs_SourceExternal)

	default:
		return nil
	}
}

func (t *RepoManifest_Source) MarshalCBOR(w io.Writer) error {

	if t == nil {
		_, err := w.Write(cbg.CborNull)
		return err
	}
	if t.RepoDefs_SourceTangled != nil {
		return t.RepoDefs_SourceTangled.MarshalCBOR(w)
	}
	if t.RepoDefs_SourceExternal != nil {
		return t.RepoDefs_SourceExternal.MarshalCBOR(w)
	}
	return fmt.Errorf("cannot cbor marshal empty enum")
}
func (t *RepoManifest_Source) UnmarshalCBOR(r io.Reader) error {
	typ, b, err := util.CborTypeExtractReader(r)
	if err != nil {
		return err
	}

	switch typ {
	case "org.tangled.repo.defs#sourceTangled":
		t.RepoDefs_SourceTangled = new(RepoDefs_SourceTangled)
		return t.RepoDefs_SourceTangled.UnmarshalCBOR(bytes.NewReader(b))
	case "org.tangled.repo.defs#sourceExternal":
		t.RepoDefs_SourceExternal = new(RepoDefs_SourceExternal)
		return t.RepoDefs_SourceExternal.UnmarshalCBOR(bytes.NewReader(b))

	default:
		return nil
	}
}
