// Code generated by apbr generate. DO NOT EDIT.
// versions:
// 	apbr generate v1.2

//go:build !codeanalysis

package model

import (
	"github.com/apibrew/apibrew/pkg/abs"
	"github.com/apibrew/apibrew/pkg/formats/unstructured"
	"github.com/apibrew/apibrew/pkg/model"
	"github.com/apibrew/apibrew/pkg/types"
	"google.golang.org/protobuf/types/known/structpb"
)

import "github.com/google/uuid"

type FileMapper struct {
}

func NewFileMapper() *FileMapper {
	return &FileMapper{}
}

var FileMapperInstance = NewFileMapper()

func (m *FileMapper) New() *File {
	return &File{}
}

func (m *FileMapper) ResourceIdentity() abs.ResourceIdentity {
	return abs.ResourceIdentity{
		Namespace: "storage",
		Name:      "File",
	}
}

func (m *FileMapper) ToRecord(file *File) *model.Record {
	var rec = &model.Record{}
	rec.Properties = m.ToProperties(file)
	return rec
}

func (m *FileMapper) FromRecord(record *model.Record) *File {
	return m.FromProperties(record.Properties)
}

func (m *FileMapper) ToProperties(file *File) map[string]*structpb.Value {
	var properties = make(map[string]*structpb.Value)

	var_Id := file.Id

	if var_Id != nil {
		var var_Id_mapped *structpb.Value

		var var_Id_err error
		var_Id_mapped, var_Id_err = types.ByResourcePropertyType(model.ResourceProperty_UUID).Pack(*var_Id)
		if var_Id_err != nil {
			panic(var_Id_err)
		}
		properties["id"] = var_Id_mapped
	}

	var_Name := file.Name

	var var_Name_mapped *structpb.Value

	var var_Name_err error
	var_Name_mapped, var_Name_err = types.ByResourcePropertyType(model.ResourceProperty_STRING).Pack(var_Name)
	if var_Name_err != nil {
		panic(var_Name_err)
	}
	properties["name"] = var_Name_mapped

	var_UploadUrl := file.UploadUrl

	var var_UploadUrl_mapped *structpb.Value

	var var_UploadUrl_err error
	var_UploadUrl_mapped, var_UploadUrl_err = types.ByResourcePropertyType(model.ResourceProperty_STRING).Pack(var_UploadUrl)
	if var_UploadUrl_err != nil {
		panic(var_UploadUrl_err)
	}
	properties["uploadUrl"] = var_UploadUrl_mapped

	var_DownloadUrl := file.DownloadUrl

	var var_DownloadUrl_mapped *structpb.Value

	var var_DownloadUrl_err error
	var_DownloadUrl_mapped, var_DownloadUrl_err = types.ByResourcePropertyType(model.ResourceProperty_STRING).Pack(var_DownloadUrl)
	if var_DownloadUrl_err != nil {
		panic(var_DownloadUrl_err)
	}
	properties["downloadUrl"] = var_DownloadUrl_mapped

	var_Version := file.Version

	var var_Version_mapped *structpb.Value

	var var_Version_err error
	var_Version_mapped, var_Version_err = types.ByResourcePropertyType(model.ResourceProperty_INT32).Pack(var_Version)
	if var_Version_err != nil {
		panic(var_Version_err)
	}
	properties["version"] = var_Version_mapped
	return properties
}

func (m *FileMapper) FromProperties(properties map[string]*structpb.Value) *File {
	var s = m.New()
	if properties["id"] != nil && properties["id"].AsInterface() != nil {

		var_Id := properties["id"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_UUID).UnPack(var_Id)

		if err != nil {
			panic(err)
		}

		var_Id_mapped := new(uuid.UUID)
		*var_Id_mapped = val.(uuid.UUID)

		s.Id = var_Id_mapped
	}
	if properties["name"] != nil && properties["name"].AsInterface() != nil {

		var_Name := properties["name"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_STRING).UnPack(var_Name)

		if err != nil {
			panic(err)
		}

		var_Name_mapped := val.(string)

		s.Name = var_Name_mapped
	}
	if properties["uploadUrl"] != nil && properties["uploadUrl"].AsInterface() != nil {

		var_UploadUrl := properties["uploadUrl"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_STRING).UnPack(var_UploadUrl)

		if err != nil {
			panic(err)
		}

		var_UploadUrl_mapped := val.(string)

		s.UploadUrl = var_UploadUrl_mapped
	}
	if properties["downloadUrl"] != nil && properties["downloadUrl"].AsInterface() != nil {

		var_DownloadUrl := properties["downloadUrl"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_STRING).UnPack(var_DownloadUrl)

		if err != nil {
			panic(err)
		}

		var_DownloadUrl_mapped := val.(string)

		s.DownloadUrl = var_DownloadUrl_mapped
	}
	if properties["version"] != nil && properties["version"].AsInterface() != nil {

		var_Version := properties["version"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_INT32).UnPack(var_Version)

		if err != nil {
			panic(err)
		}

		var_Version_mapped := val.(int32)

		s.Version = var_Version_mapped
	}
	return s
}

func (m *FileMapper) ToUnstructured(file *File) unstructured.Unstructured {
	var properties unstructured.Unstructured = make(unstructured.Unstructured)
	properties["type"] = "storage/File"

	var_Id := file.Id

	if var_Id != nil {
		var var_Id_mapped interface{}

		var_Id_mapped = var_Id.String()
		properties["id"] = var_Id_mapped
	}

	var_Name := file.Name

	var var_Name_mapped interface{}

	var_Name_mapped = var_Name
	properties["name"] = var_Name_mapped

	var_UploadUrl := file.UploadUrl

	var var_UploadUrl_mapped interface{}

	var_UploadUrl_mapped = var_UploadUrl
	properties["uploadUrl"] = var_UploadUrl_mapped

	var_DownloadUrl := file.DownloadUrl

	var var_DownloadUrl_mapped interface{}

	var_DownloadUrl_mapped = var_DownloadUrl
	properties["downloadUrl"] = var_DownloadUrl_mapped

	var_Version := file.Version

	var var_Version_mapped interface{}

	var_Version_mapped = var_Version
	properties["version"] = var_Version_mapped

	return properties
}
