Skip to content

Commit

Permalink
Merge pull request YaoApp#475 from trheyi/main
Browse files Browse the repository at this point in the history
[add] page config
  • Loading branch information
trheyi authored Oct 9, 2023
2 parents 40af69d + 2e77453 commit 3c111bc
Show file tree
Hide file tree
Showing 9 changed files with 434 additions and 28 deletions.
9 changes: 5 additions & 4 deletions sui/api/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,9 @@ func TestEditorRender(t *testing.T) {
t.Fatal(err)
}

assert.IsType(t, &core.ResponseEditor{}, res)
assert.NotEmpty(t, res.(*core.ResponseEditor).HTML)
assert.IsType(t, &core.ResponseEditorRender{}, res)
assert.NotEmpty(t, res.(*core.ResponseEditorRender).HTML)
assert.NotEmpty(t, res.(*core.ResponseEditorRender).Config)
}

func TestEditorRenderWithQuery(t *testing.T) {
Expand All @@ -563,8 +564,8 @@ func TestEditorRenderWithQuery(t *testing.T) {
t.Fatal(err)
}

assert.IsType(t, &core.ResponseEditor{}, res)
assert.NotEmpty(t, res.(*core.ResponseEditor).HTML)
assert.IsType(t, &core.ResponseEditorRender{}, res)
assert.NotEmpty(t, res.(*core.ResponseEditorRender).HTML)
}

func TestEditorPageSource(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions sui/core/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
)

// EditorRender render HTML for the editor
func (page *Page) EditorRender(request *Request) (*ResponseEditor, error) {
func (page *Page) EditorRender(request *Request) (*ResponseEditorRender, error) {

html := page.Codes.HTML.Code
res := &ResponseEditor{
res := &ResponseEditorRender{
HTML: "",
CSS: page.Codes.CSS.Code,
Scripts: []string{},
Styles: []string{},
Warnings: []string{},
Config: page.GetConfig(),
Setting: map[string]interface{}{},
}

Expand Down
3 changes: 2 additions & 1 deletion sui/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ type IPage interface {
Load() error

Get() *Page
GetConfig() *PageConfig
Save(request *RequestSource) error
SaveTemp(request *RequestSource) error
Remove() error

EditorRender(request *Request) (*ResponseEditor, error)
EditorRender(request *Request) (*ResponseEditorRender, error)
EditorPageSource() SourceData
EditorScriptSource() SourceData
EditorStyleSource() SourceData
Expand Down
61 changes: 61 additions & 0 deletions sui/core/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package core

import (
jsoniter "github.com/json-iterator/go"
)

// UnmarshalJSON Custom JSON unmarshal function for PageMock
func (mock *PageMock) UnmarshalJSON(data []byte) error {

if mock == nil {
return nil
}

type Alias struct {
Method string `json:"method"`
Params map[string]string `json:"params,omitempty"`
Query map[string]interface{} `json:"query,omitempty"`
Headers map[string]interface{} `json:"headers,omitempty"`
Body interface{} `json:"body,omitempty"`
}

aux := &Alias{}
if err := jsoniter.Unmarshal(data, &aux); err != nil {
return err
}

method := aux.Method
if method == "" {
method = "GET"
}
mock.Body = aux.Body
mock.Method = method
mock.Params = aux.Params
mock.Query = convertRecordToMap(aux.Query)
mock.Headers = convertRecordToMap(aux.Headers)
return nil
}

// Helper function to convert TypeScript Record<string, string | string[]> to map[string][]string
func convertRecordToMap(record map[string]interface{}) map[string][]string {
if record == nil {
return nil
}

result := make(map[string][]string)
for key, value := range record {
switch v := value.(type) {
case string:
result[key] = []string{v}
case []interface{}:
strValues := make([]string, len(v))
for i, item := range v {
if str, ok := item.(string); ok {
strValues[i] = str
}
}
result[key] = strValues
}
}
return result
}
228 changes: 228 additions & 0 deletions sui/core/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package core

import (
"reflect"
"testing"

jsoniter "github.com/json-iterator/go"
)

func TestRequestSourceUnmarshalJSON(t *testing.T) {
testCases := []struct {
name string
jsonData string
expected *RequestSource
shouldFail bool
}{
{
name: "Valid JSON with string query and headers",
jsonData: `{
"uid": "123",
"mock": {
"method": "GET",
"query": {
"q1": "value1",
"q2": "value2"
},
"headers": {
"header1": "value1",
"header2": "value2"
}
}
}`,
expected: &RequestSource{
UID: "123",
Mock: &PageMock{
Method: "GET",
Query: map[string][]string{
"q1": {"value1"},
"q2": {"value2"},
},
Headers: map[string][]string{
"header1": {"value1"},
"header2": {"value2"},
},
},
},
shouldFail: false,
},
{
name: "Valid JSON with array query and headers",
jsonData: `{
"uid": "456",
"mock": {
"method": "POST",
"query": {
"q1": ["value1", "value2"],
"q2": ["value3"]
},
"headers": {
"header1": ["value1", "value2"],
"header2": "value3"
}
}
}`,
expected: &RequestSource{
UID: "456",
Mock: &PageMock{
Method: "POST",
Query: map[string][]string{
"q1": {"value1", "value2"},
"q2": {"value3"},
},
Headers: map[string][]string{
"header1": {"value1", "value2"},
"header2": {"value3"},
},
},
},
shouldFail: false,
},
{
name: "Valid JSON with invalid query",
jsonData: `{
"uid": "789",
"mock": {
"method": "PUT",
"query":"1203"
}
}`,
expected: nil,
shouldFail: true,
},
// Add more test cases here to cover other scenarios.
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
var requestSource RequestSource
err := jsoniter.Unmarshal([]byte(testCase.jsonData), &requestSource)

if testCase.shouldFail {
if err == nil {
t.Errorf("%s: Expected unmarshal to fail, but it succeeded", testCase.name)
}
} else {
if err != nil {
t.Errorf("Unmarshal failed: %v", err)
}
if !reflect.DeepEqual(requestSource, *testCase.expected) {
t.Errorf("Unmarshaled result does not match expected result")
}
}
})
}
}

func TestPageConfigUnmarshalJSON(t *testing.T) {
testCases := []struct {
name string
jsonData string
expected *PageConfig
shouldFail bool
}{
{
name: "Valid JSON with PageSetting and PageMock",
jsonData: `{
"title": "Page Title",
"mock": {
"method": "GET",
"query": {
"q1": "value1",
"q2": "value2"
},
"headers": {
"header1": "value1",
"header2": "value2"
}
}
}`,
expected: &PageConfig{
PageSetting: PageSetting{
Title: "Page Title",
},
Mock: &PageMock{
Method: "GET",
Query: map[string][]string{
"q1": {"value1"},
"q2": {"value2"},
},
Headers: map[string][]string{
"header1": {"value1"},
"header2": {"value2"},
},
},
},
shouldFail: false,
},
{
name: "Valid JSON with PageSetting only",
jsonData: `{
"title": "Page Title"
}`,
expected: &PageConfig{
PageSetting: PageSetting{
Title: "Page Title",
},
Mock: nil,
},
shouldFail: false,
},
{
name: "Valid JSON with PageMock only",
jsonData: `{
"mock": {
"method": "GET",
"query": {
"q1": "value1",
"q2": "value2"
}
}
}`,
expected: &PageConfig{
Mock: &PageMock{
Method: "GET",
Query: map[string][]string{
"q1": {"value1"},
"q2": {"value2"},
},
},
},
shouldFail: false,
},
{
name: "Valid JSON with invalid query",
jsonData: `{
"title": "Page Title",
"mock": {
"method": "PUT",
"query": "invalid query"
}
}`,
expected: nil,
shouldFail: true,
},
// Add more test cases here to cover other scenarios.
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
var pageConfig PageConfig
err := jsoniter.Unmarshal([]byte(testCase.jsonData), &pageConfig)

if testCase.shouldFail {
if err == nil {
t.Errorf("%s: Expected unmarshal to fail, but it succeeded", testCase.name)
}
} else {

if err != nil {
t.Errorf("Unmarshal failed: %v", err)
}
if !reflect.DeepEqual(pageConfig, *testCase.expected) {
t.Errorf("Unmarshaled result does not match expected result")
}
}
})
}
}
30 changes: 13 additions & 17 deletions sui/core/page.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package core

import jsoniter "github.com/json-iterator/go"

// Get get the base info
func (page *Page) Get() *Page {
return page
}

// GetHTML get the html
func (page *Page) GetHTML() {}

// GetScript get the script
func (page *Page) GetScript() {}

// GetStyle get the style
func (page *Page) GetStyle() {}

// GetData get the data
func (page *Page) GetData() {}

// SaveTemp save the temp
func (page *Page) SaveTemp() {}

// Save the page
func (page *Page) Save() {}
// GetConfig get the config
func (page *Page) GetConfig() *PageConfig {
if page.Config == nil && page.Codes.CONF.Code != "" {
var config PageConfig
err := jsoniter.Unmarshal([]byte(page.Codes.CONF.Code), &config)
if err == nil {
page.Config = &config
}
}
return page.Config
}
Loading

0 comments on commit 3c111bc

Please sign in to comment.