-
-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Scala generator to playground (#1773)
Co-authored-by: Ashmit JaiSarita Gupta <[email protected]>
- Loading branch information
1 parent
d9f8592
commit eb34832
Showing
10 changed files
with
234 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
modelina-website/src/components/playground/options/ScalaGeneratorOptions.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import React, { useCallback, useContext, useEffect, useState } from 'react'; | ||
import Select from '@/components/Select'; | ||
import { PlaygroundScalaConfigContext } from '@/components/contexts/PlaygroundConfigContext'; | ||
import InfoModal from '@/components/InfoModal'; | ||
import { debounce } from 'lodash'; | ||
|
||
interface ScalaGeneratorOptionsProps { | ||
setNewConfig?: ( | ||
queryKey: string, | ||
queryValue: any, | ||
updateCode?: boolean | ||
) => void; | ||
} | ||
|
||
interface ScalaGeneratorState { | ||
packageName?: string; | ||
} | ||
|
||
export const defaultState: ScalaGeneratorState = {}; | ||
|
||
const ScalaGeneratorOptions: React.FC<ScalaGeneratorOptionsProps> = ({ | ||
setNewConfig | ||
}) => { | ||
const context = useContext(PlaygroundScalaConfigContext); | ||
const [state, setState] = useState<ScalaGeneratorState>(defaultState); | ||
|
||
useEffect(() => { | ||
setState({ ...state, packageName: context?.scalaPackageName }); | ||
}, [context?.scalaPackageName]); | ||
|
||
const debouncedSetNewConfig = debounce( | ||
(queryKey: string, queryValue: any) => setNewConfig?.(queryKey, queryValue), | ||
500 | ||
); | ||
|
||
const onChangeCollectionType = (collectionType: string) => { | ||
setNewConfig && setNewConfig('scalaCollectionType', collectionType); | ||
}; | ||
|
||
const onChangePackageName = useCallback( | ||
(event: React.ChangeEvent<HTMLInputElement>) => { | ||
const packageName = event.target.value; | ||
setState({ ...state, packageName }); | ||
setNewConfig && debouncedSetNewConfig('scalaPackageName', packageName); | ||
}, | ||
[setNewConfig, debouncedSetNewConfig, state] | ||
); | ||
|
||
return ( | ||
<ul className="flex flex-col"> | ||
<h3 className="py-2 w-full text-left border-b-[1px] border-gray-700"> | ||
Scala Specific options | ||
</h3> | ||
<li className="flex gap-1 items-center"> | ||
<InfoModal text="Package Name :"> | ||
<p> | ||
In Scala, a package name is a way to organize and group related | ||
classes, objects, and traits together. It is a naming convention | ||
that helps prevent naming conflicts and provides a hierarchical | ||
structure to the Scala codebase. | ||
</p> | ||
</InfoModal> | ||
<label className="flex flex-grow gap-1 items-center py-2 justify-between cursor-pointer"> | ||
<span className="mt-1 max-w-2xl text-sm text-gray-500"> | ||
Scala Package Name | ||
</span> | ||
<input | ||
type="text" | ||
className="form-input w-[88%] rounded-md border-gray-300 cursor-pointer font-regular text-md text-gray-700 hover:bg-gray-50 focus-within:text-gray-900" | ||
name="scalaPackageName" | ||
value={state?.packageName} | ||
onChange={onChangePackageName} | ||
/> | ||
</label> | ||
</li> | ||
<li className="flex gap-1 items-center"> | ||
<InfoModal text="Scala collection type: "> | ||
<p> | ||
It indicates the collection type. Its value can be either List or | ||
Array. | ||
<br /> <br /> | ||
The default value is Array. | ||
</p> | ||
</InfoModal> | ||
<label className="flex flex-grow gap-1 items-center py-2 justify-between cursor-pointer"> | ||
<span className="mt-1 max-w-2xl text-sm text-gray-500"> | ||
Scala collection type | ||
</span> | ||
<Select | ||
options={[ | ||
{ value: 'array', text: 'Array' }, | ||
{ value: 'list', text: 'List' } | ||
]} | ||
value={context?.scalaCollectionType} | ||
onChange={onChangeCollectionType} | ||
className="shadow-outline-blue cursor-pointer" | ||
/> | ||
</label> | ||
</li> | ||
</ul> | ||
); | ||
}; | ||
|
||
export default ScalaGeneratorOptions; |
46 changes: 46 additions & 0 deletions
46
modelina-website/src/helpers/GeneratorCode/ScalaGenerator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { ModelinaScalaOptions } from '@/types'; | ||
import { | ||
getGeneralGeneratorCode, | ||
renderGeneratorInstanceCode | ||
} from './GeneralGenerator'; | ||
|
||
export function getScalaGeneratorCode(generatorOptions: ModelinaScalaOptions) { | ||
const optionString: string[] = getGeneralGeneratorCode( | ||
generatorOptions, | ||
'scalaDefaultEnumKeyConstraints', | ||
'scalaDefaultPropertyKeyConstraints', | ||
'scalaDefaultModelNameConstraints' | ||
); | ||
const optionStringPresets: string[] = []; | ||
|
||
if (generatorOptions.showTypeMappingExample === true) { | ||
optionString.push(`typeMapping: { | ||
Integer: ({ dependencyManager, constrainedModel, options, partOfProperty }) => { | ||
// Add custom dependency for your type if required. | ||
dependencyManager.addDependency('mod my;'); | ||
//Return the type for the integer model | ||
return 'my::IntegerType'; | ||
} | ||
}`); | ||
} | ||
|
||
const generateInstanceCode = renderGeneratorInstanceCode( | ||
optionString, | ||
optionStringPresets, | ||
'ScalaGenerator' | ||
); | ||
|
||
return `// Use the following code as starting point | ||
// To generate the models exactly as displayed in the playground | ||
import { | ||
ScalaGenerator, | ||
IndentationTypes, | ||
FormatHelpers, | ||
scalaDefaultEnumKeyConstraints, | ||
scalaDefaultModelNameConstraints, | ||
scalaDefaultPropertyKeyConstraints | ||
} from '@asyncapi/modelina'; | ||
${generateInstanceCode}`; | ||
} |
39 changes: 39 additions & 0 deletions
39
modelina-website/src/pages/api/functions/ScalaGenerator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ModelProps, ModelinaScalaOptions } from "@/types"; | ||
import { DeepPartial } from "../../../../../lib/types/utils"; | ||
import { scalaDefaultEnumKeyConstraints, scalaDefaultModelNameConstraints, scalaDefaultPropertyKeyConstraints, ScalaGenerator, ScalaOptions } from "../../../../../"; | ||
import { applyGeneralOptions, convertModelsToProps } from "./Helpers"; | ||
|
||
/** | ||
* This is the server side part of the Scala generator, that takes input and generator parameters to generate the models. | ||
*/ | ||
|
||
export async function getScalaModels( | ||
input:any, | ||
generatorOptions: ModelinaScalaOptions | ||
): Promise<ModelProps[]> { | ||
const options: DeepPartial<ScalaOptions> = { | ||
presets: [] | ||
}; | ||
applyGeneralOptions(generatorOptions, options, scalaDefaultEnumKeyConstraints, scalaDefaultPropertyKeyConstraints, scalaDefaultModelNameConstraints); | ||
|
||
if(generatorOptions.showTypeMappingExample) { | ||
options.typeMapping = { | ||
Integer: ({ dependencyManager }) => { | ||
dependencyManager.addDependency('mod my;'); | ||
return 'my::IntegerType'; | ||
} | ||
} | ||
} | ||
|
||
try { | ||
const generator = new ScalaGenerator(options); | ||
const generatedModels = await generator.generateCompleteModels(input, { | ||
packageName: generatorOptions.scalaPackageName ?? 'asyncapi.models' | ||
}); | ||
return convertModelsToProps(generatedModels); | ||
} catch (e : any) { | ||
console.error('Could not generate models'); | ||
console.error(e); | ||
return e.message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters