Skip to content

Commit

Permalink
feat: add protobufs
Browse files Browse the repository at this point in the history
  • Loading branch information
bounoable committed Sep 14, 2022
1 parent 8ebf7b9 commit e6ee16a
Show file tree
Hide file tree
Showing 21 changed files with 959 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"protoc": {
"options": ["-I ./api/proto"]
}
}
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.PHONY: generate
generate:
@./scripts/generate
76 changes: 76 additions & 0 deletions api/proto/gen/gallery/v0/gallery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package gallerypb

import (
imagepb "github.com/modernice/media-entity/api/proto/gen/image/v0"
"github.com/modernice/media-entity/gallery"
"github.com/modernice/media-entity/internal/slicex"
)

type StringID string

func (id StringID) String() string {
return string(id)
}

func newStringID(id string) StringID {
return StringID(id)
}

func New[StackID, ImageID gallery.ID](g gallery.DTO[StackID, ImageID]) *Gallery {
return &Gallery{
Stacks: slicex.Map(g.Stacks, NewStack[StackID, ImageID]),
}
}

func AsGallery[StackID, ImageID gallery.ID](g *Gallery, toStackID func(string) StackID, toImageID func(string) ImageID) gallery.DTO[StackID, ImageID] {
return gallery.DTO[StackID, ImageID]{
Stacks: slicex.Map(g.GetStacks(), func(s *Stack) gallery.Stack[StackID, ImageID] {
return AsStack(s, toStackID, toImageID)
}),
}
}

func (g *Gallery) AsGallery() gallery.DTO[StringID, StringID] {
return AsGallery(g, newStringID, newStringID)
}

func NewStack[StackID, ImageID gallery.ID](s gallery.Stack[StackID, ImageID]) *Stack {
return &Stack{
Id: s.ID.String(),
Variants: slicex.Map(s.Variants, NewVariant[ImageID]),
Tags: s.Tags,
}
}

func AsStack[StackID, ImageID gallery.ID](s *Stack, toStackID func(string) StackID, toImageID func(string) ImageID) gallery.Stack[StackID, ImageID] {
return gallery.Stack[StackID, ImageID]{
ID: toStackID(s.GetId()),
Variants: slicex.Map(s.GetVariants(), func(img *Image) gallery.Image[ImageID] {
return AsImage(img, toImageID)
}),
}
}

func (s *Stack) AsStack() gallery.Stack[StringID, StringID] {
return AsStack(s, newStringID, newStringID)
}

func NewVariant[ID gallery.ID](img gallery.Image[ID]) *Image {
return &Image{
Image: imagepb.New(img.Image),
Id: img.ID.String(),
Original: img.Original,
}
}

func AsImage[ID gallery.ID](img *Image, toImageID func(string) ID) gallery.Image[ID] {
return gallery.Image[ID]{
Image: img.GetImage().AsImage(),
ID: toImageID(img.GetId()),
Original: img.GetOriginal(),
}
}

func (img *Image) AsImage() gallery.Image[StringID] {
return AsImage(img, newStringID)
}
Loading

0 comments on commit e6ee16a

Please sign in to comment.