Skip to content

Commit

Permalink
Merge pull request #469 from trheyi/main
Browse files Browse the repository at this point in the history
[add] template locale, theme, block, component api
  • Loading branch information
trheyi authored Sep 28, 2023
2 parents 7cebf75 + f0ccba6 commit 04e4f31
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 5 deletions.
49 changes: 46 additions & 3 deletions sui/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,61 @@ var dsl = []byte(`
"group": "__yao/sui/v1",
"paths": [
{
"path": "/:id/template",
"method": "GET",
"process": "sui.Template.Get",
"in": ["$param.id"],
"out": { "status": 200, "type": "application/json" }
},{
"path": "/:id/template/:template_id",
"method": "GET",
"process": "sui.Template.Find",
"in": ["$param.id", "$param.template_id"],
"out": { "status": 200, "type": "application/json" }
},
{
"path": "/:id/template/:template_id/locale",
"method": "GET",
"process": "sui.Template.Locale.Get",
"in": ["$param.id", "$param.template_id"],
"out": { "status": 200, "type": "application/json" }
},{
"path": "/:id/template",
"path": "/:id/template/:template_id/theme",
"method": "GET",
"process": "sui.Template.Get",
"in": ["$param.id"],
"process": "sui.Template.Theme.Get",
"in": ["$param.id", "$param.template_id"],
"out": { "status": 200, "type": "application/json" }
},
{
"path": "/:id/template/:template_id/block",
"method": "GET",
"process": "sui.Template.Block.Get",
"in": ["$param.id", "$param.template_id"],
"out": { "status": 200, "type": "application/json" }
},{
"path": "/:id/template/:template_id/block/:block_id",
"method": "GET",
"process": "sui.Template.Block.Find",
"in": ["$param.id", "$param.template_id", "$param.block_id"],
"out": { "status": 200, "type": "text/javascript" }
},
{
"path": "/:id/template/:template_id/component",
"method": "GET",
"process": "sui.Template.Component.Get",
"in": ["$param.id", "$param.template_id"],
"out": { "status": 200, "type": "application/json" }
},{
"path": "/:id/template/:template_id/component/:component_id",
"method": "GET",
"process": "sui.Template.Component.Find",
"in": ["$param.id", "$param.template_id", "$param.component_id"],
"out": { "status": 200, "type": "text/javascript" }
},
{
"path": "/:id/editor/render/:template_id/*route",
Expand Down
115 changes: 113 additions & 2 deletions sui/api/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ import (

func init() {
process.RegisterGroup("sui", map[string]process.Handler{
"template.get": TemplateGet,
"template.find": TemplateFind,
"template.get": TemplateGet,
"template.find": TemplateFind,
"template.locale.get": TemplateLocaleGet,
"template.theme.get": TemplateThemeGet,
"template.block.get": TemplateBlockGet,
"template.block.find": TemplateBlockFind,
"template.component.get": TemplateComponentGet,
"template.component.find": TemplateComponentFind,

"editor.render": EditorRender,
"editor.source": EditorSource,
Expand Down Expand Up @@ -42,6 +48,111 @@ func TemplateFind(process *process.Process) interface{} {
return template
}

// TemplateLocaleGet handle the find Template request
func TemplateLocaleGet(process *process.Process) interface{} {
process.ValidateArgNums(2)

sui := get(process)
template, err := sui.GetTemplate(process.ArgsString(1))
if err != nil {
exception.New(err.Error(), 500).Throw()
}

return template.Locales()
}

// TemplateThemeGet handle the find Template request
func TemplateThemeGet(process *process.Process) interface{} {
process.ValidateArgNums(2)

sui := get(process)
template, err := sui.GetTemplate(process.ArgsString(1))
if err != nil {
exception.New(err.Error(), 500).Throw()
}

return template.Themes()
}

// TemplateBlockGet handle the find Template request
func TemplateBlockGet(process *process.Process) interface{} {
process.ValidateArgNums(2)

sui := get(process)
templateID := process.ArgsString(1)

tmpl, err := sui.GetTemplate(templateID)
if err != nil {
exception.New(err.Error(), 500).Throw()
}

blocks, err := tmpl.Blocks()
if err != nil {
exception.New(err.Error(), 500).Throw()
}
return blocks
}

// TemplateBlockFind handle the find Template request
func TemplateBlockFind(process *process.Process) interface{} {
process.ValidateArgNums(3)

sui := get(process)
templateID := process.ArgsString(1)
blockID := process.ArgsString(2)

tmpl, err := sui.GetTemplate(templateID)
if err != nil {
exception.New(err.Error(), 500).Throw()
}

block, err := tmpl.Block(blockID)
if err != nil {
exception.New(err.Error(), 500).Throw()
}

return block.Source()
}

// TemplateComponentGet handle the find Template request
func TemplateComponentGet(process *process.Process) interface{} {
process.ValidateArgNums(2)

sui := get(process)
templateID := process.ArgsString(1)

tmpl, err := sui.GetTemplate(templateID)
if err != nil {
exception.New(err.Error(), 500).Throw()
}

components, err := tmpl.Components()
if err != nil {
exception.New(err.Error(), 500).Throw()
}
return components
}

// TemplateComponentFind handle the find Template request
func TemplateComponentFind(process *process.Process) interface{} {
process.ValidateArgNums(3)

sui := get(process)
templateID := process.ArgsString(1)
componentID := process.ArgsString(2)

tmpl, err := sui.GetTemplate(templateID)
if err != nil {
exception.New(err.Error(), 500).Throw()
}

component, err := tmpl.Component(componentID)
if err != nil {
exception.New(err.Error(), 500).Throw()
}
return component.Source()
}

// EditorRender handle the render page request
func EditorRender(process *process.Process) interface{} {
process.ValidateArgNums(3)
Expand Down
126 changes: 126 additions & 0 deletions sui/api/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,132 @@ func TestTemplateFind(t *testing.T) {
assert.Equal(t, "tech-blue", res.(*local.Template).ID)
}

func TestTemplateLocaleGet(t *testing.T) {
load(t)
defer clean()

// test demo
p, err := process.Of("sui.template.locale.get", "demo", "tech-blue")
if err != nil {
t.Fatal(err)
}

res, err := p.Exec()
if err != nil {
t.Fatal(err)
}

assert.IsType(t, []core.SelectOption{}, res)
assert.Equal(t, 3, len(res.([]core.SelectOption)))
assert.Equal(t, "ar", res.([]core.SelectOption)[0].Value)
assert.Equal(t, "zh-cn", res.([]core.SelectOption)[1].Value)
assert.Equal(t, "zh-tw", res.([]core.SelectOption)[2].Value)
}

func TestTemplateThemeGet(t *testing.T) {
load(t)
defer clean()

// test demo
p, err := process.Of("sui.template.theme.get", "demo", "tech-blue")
if err != nil {
t.Fatal(err)
}

res, err := p.Exec()
if err != nil {
t.Fatal(err)
}

assert.IsType(t, []core.SelectOption{}, res)
assert.Equal(t, 2, len(res.([]core.SelectOption)))
assert.Equal(t, "dark", res.([]core.SelectOption)[0].Value)
assert.Equal(t, "light", res.([]core.SelectOption)[1].Value)
}

func TestTemplateBlockGet(t *testing.T) {
load(t)
defer clean()

// test demo
p, err := process.Of("sui.template.block.get", "demo", "tech-blue")
if err != nil {
t.Fatal(err)
}

res, err := p.Exec()
if err != nil {
t.Fatal(err)
}

assert.IsType(t, []core.IBlock{}, res)
assert.Equal(t, 4, len(res.([]core.IBlock)))
assert.Equal(t, "ColumnsTwo", res.([]core.IBlock)[0].(*local.Block).ID)
assert.Equal(t, "Hero", res.([]core.IBlock)[1].(*local.Block).ID)
assert.Equal(t, "Section", res.([]core.IBlock)[2].(*local.Block).ID)
assert.Equal(t, "Table", res.([]core.IBlock)[3].(*local.Block).ID)
}

func TestTemplateBlockFind(t *testing.T) {
load(t)
defer clean()

// test demo
p, err := process.Of("sui.template.block.find", "demo", "tech-blue", "ColumnsTwo")
if err != nil {
t.Fatal(err)
}

res, err := p.Exec()
if err != nil {
t.Fatal(err)
}

assert.IsType(t, "", res)
assert.Contains(t, res.(string), "window.block__ColumnsTwo=")
}

func TestTemplateComponentGet(t *testing.T) {
load(t)
defer clean()

// test demo
p, err := process.Of("sui.template.component.get", "demo", "tech-blue")
if err != nil {
t.Fatal(err)
}

res, err := p.Exec()
if err != nil {
t.Fatal(err)
}

assert.IsType(t, []core.IComponent{}, res)
assert.Equal(t, 3, len(res.([]core.IComponent)))
assert.Equal(t, "Box", res.([]core.IComponent)[0].(*local.Component).ID)
assert.Equal(t, "Card", res.([]core.IComponent)[1].(*local.Component).ID)
assert.Equal(t, "Nav", res.([]core.IComponent)[2].(*local.Component).ID)
}

func TestTemplateComponentFind(t *testing.T) {
load(t)
defer clean()

// test demo
p, err := process.Of("sui.template.component.find", "demo", "tech-blue", "Box")
if err != nil {
t.Fatal(err)
}

res, err := p.Exec()
if err != nil {
t.Fatal(err)
}

assert.IsType(t, "", res)
assert.Contains(t, res.(string), "window.component__Box=")
}

func TestEditorRender(t *testing.T) {
load(t)
defer clean()
Expand Down
5 changes: 5 additions & 0 deletions sui/core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ func (block *Block) Compile() (string, error) {
block.Compiled = minified
return minified, nil
}

// Source get the compiled code
func (block *Block) Source() string {
return block.Compiled
}
5 changes: 5 additions & 0 deletions sui/core/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ func (component *Component) Compile() (string, error) {
component.Compiled = minified
return minified, nil
}

// Source get the compiled code
func (component *Component) Source() string {
return component.Compiled
}
2 changes: 2 additions & 0 deletions sui/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ type IPage interface {
type IBlock interface {
Compile() (string, error)
Load() error
Source() string
}

// IComponent is the interface for the component
type IComponent interface {
Compile() (string, error)
Load() error
Source() string
}

0 comments on commit 04e4f31

Please sign in to comment.