1616
1717const yaml = require ( 'yaml' ) ;
1818const ModelUtil = require ( './modelutil' ) ;
19+ const { MetaModelNamespace } = require ( '@accordproject/concerto-metamodel' ) ;
1920
2021/**
2122 * handles the target field of a command
2223 * @param {object } target the value of target
2324 * @returns {object } the simplified target object
25+ * @private
2426 */
2527function handleTarget ( target ) {
2628 const targetKeys = Object . keys ( target ) ;
@@ -42,12 +44,13 @@ function handleTarget(target){
4244 * @param {object } [argument.type] the type identifier for type reference arguments
4345 * @param {boolean } [argument.isArray] whether the type reference is an array
4446 * @returns {object } simplified argument object
47+ * @private
4548 */
4649function handleArguments ( argument ) {
4750 const mapClassToType = {
48- '[email protected] . DecoratorString' :
'String' , 49- '[email protected] . DecoratorNumber' :
'Number' , 50- '[email protected] . DecoratorBoolean' :
'Boolean' , 51+ [ ` ${ MetaModelNamespace } . DecoratorString` ] : 'String' ,
52+ [ ` ${ MetaModelNamespace } . DecoratorNumber` ] : 'Number' ,
53+ [ ` ${ MetaModelNamespace } . DecoratorBoolean` ] : 'Boolean' ,
5154 } ;
5255 if ( argument . $class . endsWith ( 'TypeReference' ) ) {
5356 return {
@@ -71,6 +74,7 @@ function handleArguments(argument){
7174 * @param {string } decorator.name the name of the decorator
7275 * @param {object[] } [decorator.arguments] the list of arguments
7376 * @returns {object } simplified decorator object with name and arguments
77+ * @private
7478 */
7579function handleDecorator ( decorator ) {
7680 return {
@@ -88,6 +92,7 @@ function handleDecorator(decorator){
8892 * @param {object } command.target the target to apply decorator to
8993 * @param {object } command.decorator the decorator to apply
9094 * @returns {object } simplified commands object
95+ * @private
9196 */
9297function handleCommands ( command ) {
9398 return {
@@ -101,7 +106,6 @@ function handleCommands(command){
101106 * converts DCS JSON to YAML string
102107 * @param {object } dcsJson the DCS JSON as parsed object
103108 * @returns {string } the DCS YAML string
104- * @throws {Error } if the input is not a valid DCS JSON
105109 */
106110function jsonToYaml ( dcsJson ) {
107111 const dcsNamespace = ModelUtil . getNamespace ( dcsJson . $class ) ;
@@ -121,4 +125,95 @@ function jsonToYaml(dcsJson){
121125 return yamlString ;
122126}
123127
124- module . exports = { jsonToYaml } ;
128+
129+ /**
130+ * handles each argument of the decorator
131+ * converts simplified argument objects back to full JSON representation
132+ * @param {string } MetaModelNamespace - the metamodel namespace
133+ * @param {object } argument - the simplified argument object
134+ * @param {string } [argument.type] - the argument type for primitive values
135+ * @param {* } [argument.value] - the argument value
136+ * @param {object } [argument.typeReference] - the type reference object for complex types
137+ * @returns {object } - the fully qualified argument object after restoring required fields
138+ * @private
139+ */
140+ function restoreArguments ( MetaModelNamespace , argument ) {
141+ const mapTypeToClass = {
142+ 'String' : `${ MetaModelNamespace } .DecoratorString` ,
143+ 'Number' : `${ MetaModelNamespace } .DecoratorNumber` ,
144+ 'Boolean' : `${ MetaModelNamespace } .DecoratorBoolean` ,
145+ } ;
146+ if ( Object . keys ( argument ) [ 0 ] === 'typeReference' ) {
147+ return {
148+ $class : ModelUtil . getFullyQualifiedName ( MetaModelNamespace , 'DecoratorTypeReference' ) ,
149+ type : {
150+ $class : ModelUtil . getFullyQualifiedName ( MetaModelNamespace , 'TypeIdentifier' ) ,
151+ name : argument . typeReference . name ,
152+ ... ( argument . typeReference . namespace !== undefined ? { namespace : argument . typeReference . namespace } : { } ) ,
153+ ... ( argument . typeReference . resolvedName !== undefined ? { resolvedName : argument . typeReference . resolvedName } : { } ) ,
154+ } ,
155+ isArray : argument . typeReference . isArray
156+ } ;
157+ }
158+ return {
159+ $class : mapTypeToClass [ argument . type ] ,
160+ value : argument . type === 'String' ? String ( argument . value ) : argument . value ,
161+ } ;
162+ }
163+
164+
165+ /**
166+ * handles the decorator of each command for yaml to json
167+ * @param {object } decorator - the decorator to convert
168+ * @returns {object } - the decorator object after restoring required fields
169+ * @private
170+ */
171+ function restoreDecorator ( decorator ) {
172+ return {
173+ $class : ModelUtil . getFullyQualifiedName ( MetaModelNamespace , 'Decorator' ) ,
174+ name : decorator . name ,
175+ arguments : decorator . arguments ? decorator . arguments . map ( ( argument ) => restoreArguments ( MetaModelNamespace , argument ) ) : [ ] ,
176+ } ;
177+ }
178+
179+
180+ /**
181+ * handles the command for yaml to json method
182+ * @param {string } dcsNamespace - the namespace of the DCS
183+ * @param {object } command - the command to convert
184+ * @returns {object } - the command object after restoring required fields
185+ * @private
186+ */
187+ function restoreCommands ( dcsNamespace , command ) {
188+ return {
189+ $class : ModelUtil . getFullyQualifiedName ( dcsNamespace , 'Command' ) ,
190+ type : command . action ,
191+ target : {
192+ $class : ModelUtil . getFullyQualifiedName ( dcsNamespace , 'CommandTarget' ) ,
193+ ... command . target
194+ } ,
195+ decorator : restoreDecorator ( command . decorator )
196+ } ;
197+ }
198+
199+
200+ /**
201+ * converts DCS YAML string to JSON format
202+ * @param {string } yamlString the YAML string to convert
203+ * @returns {object } the DCS JSON
204+ */
205+ function yamlToJson ( yamlString ) {
206+ const parsedJson = yaml . parse ( yamlString ) ;
207+ const dcsNamespace = 'org.accordproject.decoratorcommands@' + parsedJson . decoratorCommandsVersion ;
208+ const dcsJson = {
209+ $class : ModelUtil . getFullyQualifiedName ( dcsNamespace , 'DecoratorCommandSet' ) ,
210+ name : parsedJson . name ,
211+ version : parsedJson . version ,
212+ commands : parsedJson . commands . map ( ( command ) => restoreCommands ( dcsNamespace , command ) ) ,
213+ } ;
214+
215+ return dcsJson ;
216+ }
217+
218+
219+ module . exports = { jsonToYaml, yamlToJson } ;
0 commit comments