Skip to content

Commit

Permalink
feat: add script type and discriminator for attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Oct 19, 2021
1 parent 1214eee commit de0af95
Show file tree
Hide file tree
Showing 10 changed files with 568 additions and 93 deletions.
107 changes: 102 additions & 5 deletions ui/node/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type Attributes interface {

// swagger:ignore
GetValue() interface{}

// swagger:ignore
GetNodeType() Type
}

// InputAttributes represents the attributes of an input node
Expand Down Expand Up @@ -71,12 +74,11 @@ type InputAttributes struct {
// used for WebAuthn.
OnClick string `json:"onclick,omitempty"`

// OnLoad may contain javascript which should be executed on load. This is primarily
// used for WebAuthn. Using this value makes most sense when used on the server-side. For
// JavaScript apps running in the browser please load the WebAuthn JavaScript:
// NodeType represents this node's types. It is a mirror of `node.type` and
// is primarily used to allow compatibility with OpenAPI 3.0.
//
// <script src="https://public-kratos.example.org/.well-known/ory/webauthn.js" type="script" async />
OnLoad string `json:"onload,omitempty"`
// required: true
NodeType Type `json:"node_type"`
}

// ImageAttributes represents the attributes of an image node.
Expand All @@ -99,6 +101,12 @@ type ImageAttributes struct {

// Height of the image
Height int `json:"height,omitempty"`

// NodeType represents this node's types. It is a mirror of `node.type` and
// is primarily used to allow compatibility with OpenAPI 3.0.
//
// required: true
NodeType Type `json:"node_type"`
}

// AnchorAttributes represents the attributes of an anchor node.
Expand All @@ -120,6 +128,12 @@ type AnchorAttributes struct {
//
// required: true
Identifier string `json:"id"`

// NodeType represents this node's types. It is a mirror of `node.type` and
// is primarily used to allow compatibility with OpenAPI 3.0.
//
// required: true
NodeType Type `json:"node_type"`
}

// TextAttributes represents the attributes of a text node.
Expand All @@ -136,13 +150,61 @@ type TextAttributes struct {
//
// required: true
Identifier string `json:"id"`

// NodeType represents this node's types. It is a mirror of `node.type` and
// is primarily used to allow compatibility with OpenAPI 3.0.
//
// required: true
NodeType Type `json:"node_type"`
}

// ScriptAttributes represent script nodes which load javascript.
//
// swagger:model uiNodeScriptAttributes
type ScriptAttributes struct {
// The script source
//
// required: true
Source string `json:"src"`

// The script async type
//
// required: true
Async bool `json:"async"`

// The script referrer policy
//
// required: true
ReferrerPolicy string `json:"referrerpolicy"`

// The script cross origin policy
//
// required: true
CrossOrigin string `json:"crossorigin"`

// The script MIME type
//
// required: true
Type string `json:"type"`

// A unique identifier
//
// required: true
Identifier string `json:"id"`

// NodeType represents this node's types. It is a mirror of `node.type` and
// is primarily used to allow compatibility with OpenAPI 3.0.
//
// required: true
NodeType Type `json:"node_type"`
}

var (
_ Attributes = new(InputAttributes)
_ Attributes = new(ImageAttributes)
_ Attributes = new(AnchorAttributes)
_ Attributes = new(TextAttributes)
_ Attributes = new(ScriptAttributes)
)

func (a *InputAttributes) ID() string {
Expand All @@ -161,6 +223,10 @@ func (a *TextAttributes) ID() string {
return a.Identifier
}

func (a *ScriptAttributes) ID() string {
return a.Identifier
}

func (a *InputAttributes) SetValue(value interface{}) {
a.FieldValue = value
}
Expand All @@ -177,6 +243,10 @@ func (a *TextAttributes) SetValue(value interface{}) {
a.Text, _ = value.(*text.Message)
}

func (a *ScriptAttributes) SetValue(value interface{}) {
a.Source, _ = value.(string)
}

func (a *InputAttributes) GetValue() interface{} {
return a.FieldValue
}
Expand All @@ -193,6 +263,10 @@ func (a *TextAttributes) GetValue() interface{} {
return a.Text
}

func (a *ScriptAttributes) GetValue() interface{} {
return a.Source
}

func (a *InputAttributes) Reset() {
a.FieldValue = nil
}
Expand All @@ -205,3 +279,26 @@ func (a *AnchorAttributes) Reset() {

func (a *TextAttributes) Reset() {
}

func (a *ScriptAttributes) Reset() {
}

func (a *InputAttributes) GetNodeType() Type {
return a.NodeType
}

func (a *ImageAttributes) GetNodeType() Type {
return a.NodeType
}

func (a *AnchorAttributes) GetNodeType() Type {
return a.NodeType
}

func (a *TextAttributes) GetNodeType() Type {
return a.NodeType
}

func (a *ScriptAttributes) GetNodeType() Type {
return a.NodeType
}
41 changes: 41 additions & 0 deletions ui/node/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package node
import (
"testing"

"github.com/tidwall/gjson"

"github.com/ory/x/jsonx"

"github.com/stretchr/testify/assert"
)

Expand All @@ -11,4 +15,41 @@ func TestIDs(t *testing.T) {
assert.EqualValues(t, "foo", (&ImageAttributes{Identifier: "foo"}).ID())
assert.EqualValues(t, "foo", (&TextAttributes{Identifier: "foo"}).ID())
assert.EqualValues(t, "foo", (&InputAttributes{Name: "foo"}).ID())
assert.EqualValues(t, "foo", (&ScriptAttributes{Identifier: "foo"}).ID())
}

func TestNodeEncode(t *testing.T) {
script := jsonx.TestMarshalJSONString(t, &Node{Attributes: &ScriptAttributes{}})
assert.EqualValues(t, Script, gjson.Get(script, "attributes.node_type").String())
assert.EqualValues(t, Script, gjson.Get(script, "type").String())

text := jsonx.TestMarshalJSONString(t, &Node{Attributes: &TextAttributes{}})
assert.EqualValues(t, Text, gjson.Get(text, "attributes.node_type").String())
assert.EqualValues(t, Text, gjson.Get(text, "type").String())

image := jsonx.TestMarshalJSONString(t, &Node{Attributes: &ImageAttributes{}})
assert.EqualValues(t, Image, gjson.Get(image, "attributes.node_type").String())
assert.EqualValues(t, Image, gjson.Get(image, "type").String())

input := jsonx.TestMarshalJSONString(t, &Node{Attributes: &InputAttributes{}})
assert.EqualValues(t, Input, gjson.Get(input, "attributes.node_type").String())
assert.EqualValues(t, Input, gjson.Get(input, "type").String())

anchor := jsonx.TestMarshalJSONString(t, &Node{Attributes: &AnchorAttributes{}})
assert.EqualValues(t, Anchor, gjson.Get(anchor, "attributes.node_type").String())
assert.EqualValues(t, Anchor, gjson.Get(anchor, "type").String())
}

func TestNodeDecode(t *testing.T) {
for _, kind := range []Type{
Text,
Input,
Image,
Anchor,
Script,
} {
var n Node
jsonx.TestUnmarshalJSON(t, []byte(`{"type":"`+kind+`"}`), &n)
assert.EqualValues(t, kind, n.Attributes.GetNodeType())
}
}
24 changes: 16 additions & 8 deletions ui/node/fixtures/sort/expected/1.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"type": "hidden",
"value": "Sk3WySQDVpL5V9IuQelZTXcRVbBE1STI0HAJEcHOnM0/xPz5p1piBzuBMKZWxgYJc5TN8k8BTM9Hazrf/n76pg==",
"required": true,
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -19,7 +20,8 @@
"name": "oidc.provider",
"type": "submit",
"value": "github",
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -31,7 +33,8 @@
"name": "oidc.provider",
"type": "submit",
"value": "google",
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -43,7 +46,8 @@
"name": "oidc.provider",
"type": "submit",
"value": "hydra",
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -55,7 +59,8 @@
"name": "method",
"type": "submit",
"value": "oidc",
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -68,7 +73,8 @@
"type": "text",
"value": "",
"required": true,
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -80,7 +86,8 @@
"name": "password",
"type": "password",
"required": true,
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -92,7 +99,8 @@
"name": "method",
"type": "submit",
"value": "password",
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand Down
27 changes: 18 additions & 9 deletions ui/node/fixtures/sort/expected/2.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"type": "hidden",
"value": "qomtJu6UOzh5Gqkq3MEwbbJuNgJEfDg9gEowfMH0OhjfAIcWbc0PrbvMS6LL7m8ptuuuQE+oUDoXUQOy/kRccw==",
"required": true,
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -19,7 +20,8 @@
"name": "oidc.provider",
"type": "submit",
"value": "github",
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -31,7 +33,8 @@
"name": "oidc.provider",
"type": "submit",
"value": "google",
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -43,7 +46,8 @@
"name": "oidc.provider",
"type": "submit",
"value": "hydra",
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -55,7 +59,8 @@
"name": "method",
"type": "submit",
"value": "oidc",
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -66,7 +71,8 @@
"attributes": {
"name": "traits.abcd",
"type": "input",
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -78,7 +84,8 @@
"name": "password",
"type": "password",
"required": true,
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -89,7 +96,8 @@
"attributes": {
"name": "traits.email",
"type": "email",
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand All @@ -101,7 +109,8 @@
"name": "method",
"type": "submit",
"value": "password",
"disabled": false
"disabled": false,
"node_type": "input"
},
"messages": [],
"meta": {}
Expand Down

0 comments on commit de0af95

Please sign in to comment.