Skip to content

Commit

Permalink
Change to maps
Browse files Browse the repository at this point in the history
  • Loading branch information
shilgapira committed Nov 14, 2024
1 parent d1f0da9 commit 5922de0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 56 deletions.
18 changes: 9 additions & 9 deletions descope/internal/mgmt/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (r *flow) DeleteFlows(ctx context.Context, flowIDs []string) error {
return err
}

func (r *flow) ExportFlow(ctx context.Context, flowID string) (*descope.Flow, error) {
func (r *flow) ExportFlow(ctx context.Context, flowID string) (map[string]any, error) {
if flowID == "" {
return nil, utils.NewInvalidArgumentError("flowID")
}
Expand All @@ -45,27 +45,27 @@ func (r *flow) ExportFlow(ctx context.Context, flowID string) (*descope.Flow, er
return unmarshalFlow(res)
}

func (r *flow) ImportFlow(ctx context.Context, flowID string, flow *descope.Flow) error {
func (r *flow) ImportFlow(ctx context.Context, flowID string, flow map[string]any) error {
if flowID == "" {
return utils.NewInvalidArgumentError("flowID")
}
flow.FlowID = flowID
flow["flowId"] = flowID
body := map[string]any{
"flow": flow,
}
_, err := r.client.DoPostRequest(ctx, api.Routes.ManagementFlowImport(), body, nil, r.conf.ManagementKey)
return err
}

func (r *flow) ExportTheme(ctx context.Context) (*descope.Theme, error) {
func (r *flow) ExportTheme(ctx context.Context) (map[string]any, error) {
res, err := r.client.DoPostRequest(ctx, api.Routes.ManagementThemeExport(), nil, nil, r.conf.ManagementKey)
if err != nil {
return nil, err
}
return unmarshalTheme(res)
}

func (r *flow) ImportTheme(ctx context.Context, theme *descope.Theme) error {
func (r *flow) ImportTheme(ctx context.Context, theme map[string]any) error {
if theme == nil {
return utils.NewInvalidArgumentError("theme")
}
Expand All @@ -86,9 +86,9 @@ func unmarshalFlowsResponse(res *api.HTTPResponse) (*descope.FlowList, error) {
return a, nil
}

func unmarshalFlow(res *api.HTTPResponse) (*descope.Flow, error) {
func unmarshalFlow(res *api.HTTPResponse) (map[string]any, error) {
type flowResponse struct {
Flow *descope.Flow `json:"flow"`
Flow map[string]any `json:"flow"`
}
var a *flowResponse
err := utils.Unmarshal([]byte(res.BodyStr), &a)
Expand All @@ -98,9 +98,9 @@ func unmarshalFlow(res *api.HTTPResponse) (*descope.Flow, error) {
return a.Flow, nil
}

func unmarshalTheme(res *api.HTTPResponse) (*descope.Theme, error) {
func unmarshalTheme(res *api.HTTPResponse) (map[string]any, error) {
type themeResponse struct {
Theme *descope.Theme `json:"theme"`
Theme map[string]any `json:"theme"`
}
var a *themeResponse
err := utils.Unmarshal([]byte(res.BodyStr), &a)
Expand Down
25 changes: 11 additions & 14 deletions descope/internal/mgmt/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ func TestDeleteFlowsFailure(t *testing.T) {

func TestExportFlowSuccess(t *testing.T) {
flowID := "abc"
flow := &descope.Flow{
FlowID: "xyz",
Metadata: map[string]any{"foo": "bar"},
Screens: []*descope.FlowScreen{{ScreenID: "qux"}},
flow := map[string]any{
"flowId": "xyz",
"metadata": map[string]any{"foo": "bar"},
}
body := map[string]any{
"flow": flow,
Expand All @@ -71,10 +70,9 @@ func TestExportFlowMissingArgument(t *testing.T) {

func TestImportFlowSuccess(t *testing.T) {
flowID := "abc"
flow := &descope.Flow{
FlowID: "xyz",
Metadata: map[string]any{"foo": "bar"},
Screens: []*descope.FlowScreen{{ScreenID: "qux", Contents: map[string]any{}}},
flow := map[string]any{
"flowId": "xyz",
"metadata": map[string]any{"foo": "bar"},
}
mgmt := newTestMgmt(nil, helpers.DoOkWithBody(func(r *http.Request) {
require.Equal(t, r.Header.Get("Authorization"), "Bearer a:key")
Expand All @@ -84,7 +82,6 @@ func TestImportFlowSuccess(t *testing.T) {
flow := req["flow"].(map[string]any)
require.Equal(t, flowID, flow["flowId"])
require.Equal(t, map[string]any{"foo": "bar"}, flow["metadata"])
require.Equal(t, []any{map[string]any{"screenId": "qux", "contents": map[string]any{}}}, flow["screens"])
}, nil))
err := mgmt.Flow().ImportFlow(context.Background(), flowID, flow)
require.NoError(t, err)
Expand All @@ -93,13 +90,13 @@ func TestImportFlowSuccess(t *testing.T) {
func TestImportFlowMissingArgument(t *testing.T) {
flowID := ""
mgmt := newTestMgmt(nil, helpers.DoOk(nil))
err := mgmt.Flow().ImportFlow(context.Background(), flowID, &descope.Flow{})
err := mgmt.Flow().ImportFlow(context.Background(), flowID, map[string]any{})
require.ErrorContains(t, err, utils.NewInvalidArgumentError("flowID").Message)
}

func TestExportThemeSuccess(t *testing.T) {
theme := &descope.Theme{
Styles: map[string]any{"foo": "bar"},
theme := map[string]any{
"styles": map[string]any{"foo": "bar"},
}
body := map[string]any{
"theme": theme,
Expand All @@ -113,8 +110,8 @@ func TestExportThemeSuccess(t *testing.T) {
}

func TestImportThemeSuccess(t *testing.T) {
theme := &descope.Theme{
Styles: map[string]any{"foo": "bar"},
theme := map[string]any{
"styles": map[string]any{"foo": "bar"},
}
mgmt := newTestMgmt(nil, helpers.DoOkWithBody(func(r *http.Request) {
require.Equal(t, r.Header.Get("Authorization"), "Bearer a:key")
Expand Down
10 changes: 5 additions & 5 deletions descope/sdk/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,23 +643,23 @@ type Flow interface {
DeleteFlows(ctx context.Context, flowIDs []string) error

// Export a flow and its screens by the flow id.
ExportFlow(ctx context.Context, flowID string) (*descope.Flow, error)
ExportFlow(ctx context.Context, flowID string) (map[string]any, error)

// Import a flow and its screens as a given flow id. This will override the existing flow.
//
// The value of the flowID parameter will override whatever is set in the flow object.
// The value of the flowID parameter will override whatever is set in the flow data.
//
// IMPORTANT: This action is irreversible. Use carefully.
ImportFlow(ctx context.Context, flowID string, flow *descope.Flow) error
ImportFlow(ctx context.Context, flowID string, flow map[string]any) error

// Export the project theme.
ExportTheme(ctx context.Context) (*descope.Theme, error)
ExportTheme(ctx context.Context) (map[string]any, error)

// Import a given theme. This will override the existing project theme.
// Returns the new theme after a successful import or an error on failure.
//
// IMPORTANT: This action is irreversible. Use carefully.
ImportTheme(ctx context.Context, theme *descope.Theme) error
ImportTheme(ctx context.Context, theme map[string]any) error
}

// Provides functions for exporting and importing project settings, flows, styles, etc.
Expand Down
16 changes: 8 additions & 8 deletions descope/tests/mocks/mgmt/managementmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,17 +1134,17 @@ type MockFlow struct {
DeleteFlowsError error

ExportFlowAssert func(flowID string)
ExportFlowResponse *descope.Flow
ExportFlowResponse map[string]any
ExportFlowError error

ExportThemeAssert func()
ExportThemeResponse *descope.Theme
ExportThemeResponse map[string]any
ExportThemeError error

ImportFlowAssert func(flowID string, flow *descope.Flow)
ImportFlowAssert func(flowID string, flow map[string]any)
ImportFlowError error

ImportThemeAssert func(theme *descope.Theme)
ImportThemeAssert func(theme map[string]any)
ImportThemeError error
}

Expand All @@ -1162,28 +1162,28 @@ func (m *MockFlow) DeleteFlows(_ context.Context, flowIDs []string) error {
return m.DeleteFlowsError
}

func (m *MockFlow) ExportFlow(_ context.Context, flowID string) (*descope.Flow, error) {
func (m *MockFlow) ExportFlow(_ context.Context, flowID string) (map[string]any, error) {
if m.ExportFlowAssert != nil {
m.ExportFlowAssert(flowID)
}
return m.ExportFlowResponse, m.ExportFlowError
}

func (m *MockFlow) ExportTheme(_ context.Context) (*descope.Theme, error) {
func (m *MockFlow) ExportTheme(_ context.Context) (map[string]any, error) {
if m.ExportThemeAssert != nil {
m.ExportThemeAssert()
}
return m.ExportThemeResponse, m.ExportThemeError
}

func (m *MockFlow) ImportFlow(_ context.Context, flowID string, flow *descope.Flow) error {
func (m *MockFlow) ImportFlow(_ context.Context, flowID string, flow map[string]any) error {
if m.ImportFlowAssert != nil {
m.ImportFlowAssert(flowID, flow)
}
return m.ImportFlowError
}

func (m *MockFlow) ImportTheme(_ context.Context, theme *descope.Theme) error {
func (m *MockFlow) ImportTheme(_ context.Context, theme map[string]any) error {
if m.ImportThemeAssert != nil {
m.ImportThemeAssert(theme)
}
Expand Down
20 changes: 0 additions & 20 deletions descope/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,20 +824,6 @@ type Group struct {
Members []GroupMember `json:"members,omitempty"`
}

type Flow struct {
FlowID string `json:"flowId"`
Metadata map[string]any `json:"metadata"`
Contents map[string]any `json:"contents"`
Screens []*FlowScreen `json:"screens,omitempty"`
References map[string]any `json:"references,omitempty"`
}

type FlowScreen struct {
ScreenID string `json:"screenId"`
Contents map[string]any `json:"contents"`
Interactions []map[string]any `json:"interactions,omitempty"`
}

type FlowList struct {
Flows []*FlowListEnty `json:"flows"`
}
Expand All @@ -849,12 +835,6 @@ type FlowListEnty struct {
Disabled bool `json:"disabled,omitempty"`
}

type Theme struct {
Styles map[string]any `json:"styles"`
References map[string]any `json:"references,omitempty"`
ComponentsVersion string `json:"componentsVersion,omitempty"`
}

type AuditRecord struct {
ProjectID string `json:"projectId,omitempty"`
UserID string `json:"userId,omitempty"`
Expand Down

0 comments on commit 5922de0

Please sign in to comment.