Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major clean-up of code #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Major clean-up of code #1

wants to merge 6 commits into from

Conversation

daledupreezISC
Copy link

@daledupreezISC daledupreezISC commented Sep 20, 2017

  • Rename toJSON() to toDynamicObject()
  • Code formatting and consistency changes
  • Refactor population utilities
  • General code cleanup

 - Rename toJSON() to toJSONObject()
 - Code formatting and consistency changes
 - Refactor population utilities
 - General code cleanup

set tSC = $$$ADDSC(tSC,..CreateWidget("Waterproof Widget", 10.99, 10, "This widget is waterproof to a depth of 100m for up to 7 hours"))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the population tools to split up the object creation from the object definition, as well as adding better error handling and more formatting consistency.

set tSC = $$$OK
set widgetAry = []

&SQL(DECLARE WidgetCursor CURSOR FOR
Copy link
Author

@daledupreezISC daledupreezISC Sep 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Use more descriptive cursor names
  • Check SQLCODE for errors
  • Avoid reuse of Id in both cursor loops
  • Handle errors from calls to %OpenId()

QUIT tSC
set retObj = {}

if '##class(User.Widget).%ExistsId(WidgetId) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Cleanly report case where the requested widget doesn't exist
  • Cleanly handle cases where we fail to open a widget that does exist


set widgetClass = "User.Widget"
set internalWidgetId = WidgetId
if ($extract(WidgetId,1) = "W") {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not changed this, but I think we should have a dedicated widgetType property on the returned data.

SET retObj = {}
set retObj = {}

set widgetClass = "User.Widget"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Clean up widget class detection
  • Cleanly handle widgets that don't exist
  • Improve error handling for multiple issues

set retObj = {}
set tSC = $$$OK

if '##class(User.Widget).%ExistsId(WidgetId) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Cleaner handling of widgets that don't exist
  • Clean up formatting
  • Clean up error handling

do widgetAry.%Push(widgetObj.toJSON(1))
}
&SQL(CLOSE WidgetCurs)
set %response.ContentType = "application/json"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not clear why these methods are present here AND in the other class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an issue in InterSystems IRIS handling the map route to send all /widgets/... requests to the sub class. So they were moved back into the top Dispatch class.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ocorrea7, might it make more sense to have shell methods rather than duplicating the actual code?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A shell method in one of the classes that calls the class method in the other? Yeah, that's probably cleaner. Then add a comment saying this is temporary to support the current version of IRIS?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ocorrea7, I added the shell methods in the base class and an XML comment to indicate that some versions of InterSystems Cache/IRIS have a bug that prevent the Map from working as expected.

@@ -6,32 +6,34 @@ Class UnitTest.Transaction Extends UnitTest.Abstract
Method OnBeforeAllTests() As %Status
{
tstart
if ..Manager.Debug set %debug=1
if ..Manager.Debug {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code formatting

@@ -14,7 +14,7 @@ Property Barcode As %String;

Property Location As %String;

Method toJSON(traverseRelationships As %Boolean = 0) As %String
Method toJSONObject(traverseRelationships As %Boolean = 0) As %Library.DynamicObject
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change method name and signature to capture what this method is actually doing.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think calling this "toDynamicObject" (here and elsewhere) might be clearer.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -29,27 +29,25 @@ Method toJSON(traverseRelationships As %Boolean = 0) As %String
quit jsonReturn
}

Method fromJSON(json As %String) As %String
Method fromJSON(json As %String) As %Status
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Fix method signature to indicate what we were returning
  • Use classmethod directly instead of {}.%FromJSON()

@@ -14,7 +14,7 @@ Property Deleted as %Boolean;

Relationship Accessories As User.WidgetAccessoryLink [ Cardinality = many, Inverse = Widget ];

Method toJSON(traverseRelationships As %Boolean = 0) As %String
Method toJSONObject(traverseRelationships As %Boolean = 0) As %Library.DynamicObject
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Fix method name/signature
  • Clean up code and make it clearer

}

quit jsonReturn
}

Method fromJSON(json As %String) As %String
Method fromJSON(json As %String) As %Status
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Fix method signature
  • Avoid classmethod call on instance
  • Clean up formatting

@@ -20,7 +20,7 @@ Property SKU As %String;

Relationship Widgets As User.WidgetAccessoryLink [ Cardinality = many, Inverse = Accessory ];

Method toJSON(traverseRelationships As %Boolean = 0) As %String
Method toJSONObject(traverseRelationships As %Boolean = 0) As %Library.DynamicObject
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Change name/method signature
  • Code cleanup

@@ -2,7 +2,7 @@
Class Util.Build {

/// This is a build manifest. Here we describe build steps in XML format.
XData BuildManifest
XData BuildManifest [ XMLNamespace = INSTALLER ]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor code cleanup/formatting

@daledupreezISC
Copy link
Author

I've added method-level comments as to what I did for each class.

}
do widgetAry.%Push(widgetObj.toJSONObject(1))
}
if (SQLCODE && (SQLCODE '= 100)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if (SQLCODE < 0) would be clearer (here and elsewhere)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -14,7 +14,7 @@ Property Barcode As %String;

Property Location As %String;

Method toJSON(traverseRelationships As %Boolean = 0) As %String
Method toJSONObject(traverseRelationships As %Boolean = 0) As %Library.DynamicObject

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think calling this "toDynamicObject" (here and elsewhere) might be clearer.

@@ -29,27 +29,25 @@ Method toJSON(traverseRelationships As %Boolean = 0) As %String
quit jsonReturn
}

Method fromJSON(json As %String) As %String
Method fromJSON(json As %String) As %Status

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps highlight that json could also be a stream (because %FromJSON supports that - this would be handy in some of the REST APIs).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current REST code is reading from the incoming content stream, so I've left that aspect of the code in place.

@@ -4,184 +4,221 @@ Class REST.Dispatch Extends %CSP.REST

XData UrlMap [ XMLNamespace = "http://www.widgetsdirect.com/urlmap" ]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be the standard UrlMap XML namespace, http://www.intersystems.com/urlmap (here and elsewhere) - with that in place Studio will suggest the right elements/attributes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

@ocorrea7 ocorrea7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks for cleaning things up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants