Skip to content

Commit

Permalink
refactoring and some polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
titans55 committed Apr 18, 2021
1 parent 3919e0f commit 7db33be
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 122 deletions.
36 changes: 15 additions & 21 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,23 @@
<div class="container">
<div class="row">
<div class="col-12 mt-3 text-center">
<button mat-raised-button color="primary" class="mb-3 text-right" (click)="getIntentValues();initSerializer();"
<button mat-raised-button color="primary" class="mb-3 text-right" (click)="initSerializer();"
[disabled]="!nameOfTheRootSerializer">Generate</button>
</div>
<div class="col-sm-12 col-md-6 pt-1">
<div class="d-flex justify-content-between mx-3 mt-1">
<mat-form-field appearance="fill">
<mat-label>Intent</mat-label>
<mat-select [(ngModel)]="selected_intent_type">
<mat-option value="spaces">Spaces</mat-option>
<mat-option value="tabs">Tabs</mat-option>
<div class="col-sm-12 col-md-6 pt-1">
<div class="d-flex justify-content-between mt-1">
<mat-form-field appearance="outline">
<mat-label>Indent Type</mat-label>
<mat-select [(ngModel)]="selectedIndentType" (selectionChange)="setDefaultIdentSize($event)">
<mat-option [value]="indentType" *ngFor="let indentType of indentTypes">{{indentType.label}}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field appearance="fill">
<mat-label>Intent</mat-label>
<mat-select [(ngModel)]="selected_intent_size">
<mat-option value="1">1</mat-option>
<mat-option value="2">2</mat-option>
<mat-option value="3">3</mat-option>
<mat-option value="4">4</mat-option>
<mat-option value="5">5</mat-option>
<mat-option value="6">6</mat-option>
<mat-option value="7">7</mat-option>
<mat-option value="8">8</mat-option>
<mat-form-field appearance="outline">
<mat-label>Indent Size</mat-label>
<mat-select [(ngModel)]="selectedIndentSize">
<mat-option *ngFor="let option of selectedIndentType.options" [value]="option">
{{option}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
Expand All @@ -45,8 +39,8 @@
</mat-form-field>
</div>
<div class="col-sm-12 col-md-6 pt-1">
<div class="d-flex justify-content-between mx-3 mt-1">
<mat-form-field>
<div class="d-flex justify-content-between mt-1">
<mat-form-field appearance="outline">
<mat-label>Name of the root serializer</mat-label>
<input matInput [(ngModel)]="nameOfTheRootSerializer" required>
</mat-form-field>
Expand Down
93 changes: 75 additions & 18 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,79 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { JsonToSerializerParser } from './json2serializers/parser';
import { CaseEnum } from './json2serializers/case.enum';
import { IndentTypeEnum } from './json2serializers/indent-type.enum';
import { MatSelectChange } from '@angular/material/select';
import { SwUpdate } from '@angular/service-worker';
import { Subscription } from 'rxjs';

interface IndentTypeConfigs {
key: IndentTypeEnum;
label: string;
options: number[];
}


@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
export class AppComponent implements OnInit, OnDestroy {
jsonData: any = '{\n "name":"Ashlee Buckner",\n "age":27,\n "eyeColor":"green",\n "totalProfit":45.50,\n "parent":{\n "isActive":true,\n "name":"Kathleen Poole",\n "age":36,\n "eyeColor":"blue",\n "parent":{\n "name":"Tillman Ryan"\n }\n },\n "friends":[\n {"id":0,"name":"Gaines Mccall"},\n {"id":1,"name":"Gabrielle Reid"},\n {"id":2,"name":"Mcguire Macias"}\n ]\n}'
serializer: any;

parser: JsonToSerializerParser = new JsonToSerializerParser()
isSnakeCasePreferred: boolean = true
nameOfTheRootSerializer: string = "Root"
selected_intent_size: string = "4"
selected_intent_type: string = "spaces"

indentTypes: IndentTypeConfigs[] = [
{
key: IndentTypeEnum.SPACES,
label: "Spaces",
options: [
1,
2,
3,
4,
5,
6,
7,
8
]
},
{
key: IndentTypeEnum.TABS,
label: "Tabs",
options: [
1,
2
]
}
]
selectedIndentType: IndentTypeConfigs = this.indentTypes[1];
selectedIndentSize: number = this.selectedIndentType.options[0];

subscription: Subscription;

constructor(private readonly updates: SwUpdate) {
this.subscription = this.updates.available.subscribe(event => {
this.updates.activateUpdate().then(() => document.location.reload());
});
}

ngOnInit(): void {
this.initSerializer()
}

ngOnDestroy(): void {
this.subscription.unsubscribe();
}

initSerializer() {
console.log(this.jsonData)
let jsonParsed
try{
let jsonParsed
try {
jsonParsed = JSON.parse(this.jsonData)
}catch{
} catch {
alert("JSON is not valid")
return
}
Expand All @@ -38,23 +85,33 @@ export class AppComponent implements OnInit {
content: [
{
type: "text",
text: this.parser.parse(jsonParsed, (this.isSnakeCasePreferred ? CaseEnum.SNAKE_CASE : CaseEnum.NO_CHANGE), this.nameOfTheRootSerializer)
text: this.parser.parse(
jsonParsed,
{
preferredCaseForFields: (this.isSnakeCasePreferred ? CaseEnum.SNAKE_CASE : CaseEnum.NO_CHANGE),
nameOfTheRootSerializer: this.nameOfTheRootSerializer,
selectedIndentConfigs: {
selectedIndentType: this.selectedIndentType.key,
selectedIndentSize: this.selectedIndentSize
}
}
)
}
]
},
]
};

}

getIntentValues(){
let intent_type_mapping = {
'spaces': 'setIntentSpaces',
'tabs': 'setIntentTabs',
setDefaultIdentSize(selectChange: MatSelectChange) {
let selectedIdentTypeConfig: IndentTypeConfigs = selectChange.value
switch (selectedIdentTypeConfig.key) {
case IndentTypeEnum.SPACES:
this.selectedIndentSize = selectedIdentTypeConfig.options[3]
break;
case IndentTypeEnum.TABS:
this.selectedIndentSize = selectedIdentTypeConfig.options[0]
break;
}
let intent_size = parseInt(this.selected_intent_size);
this.parser[intent_type_mapping[this.selected_intent_type]](intent_size)
}


}
4 changes: 4 additions & 0 deletions src/app/json2serializers/indent-type.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum IndentTypeEnum {
SPACES = "spaces",
TABS = "tabs"
}
126 changes: 78 additions & 48 deletions src/app/json2serializers/parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { JsonToSerializerParser } from "./parser"
import { assert } from 'console'
import { CaseEnum } from './case.enum'
import { IndentTypeEnum } from './indent-type.enum'
import { JsonToSerializerParser, ParsingConfigs } from "./parser"

const parsingConfigs: ParsingConfigs = {
preferredCaseForFields: CaseEnum.SNAKE_CASE,
nameOfTheRootSerializer: "Example",
selectedIndentConfigs: {
selectedIndentType: IndentTypeEnum.SPACES,
selectedIndentSize: 3
}
}

describe('JsonToSerializerParser', () => {
let parser: JsonToSerializerParser
Expand All @@ -9,11 +19,16 @@ describe('JsonToSerializerParser', () => {
})

it('should be able to parse a simple json to drf serializer', () => {
expect(parser.parse({
name: 'Kutay',
isLazy: true,
age: 80,
})).toBe(
expect(
parser.parse(
{
name: 'Kutay',
isLazy: true,
age: 80,
},
parsingConfigs
)
).toBe(
"from rest_framework import serializers\n\n" +
"class ExampleSerializer(serializers.Serializer):\n" +
" name = serializers.CharField()\n" +
Expand All @@ -23,24 +38,34 @@ describe('JsonToSerializerParser', () => {
})

it('should be able to parse a json with array to drf serializer', () => {
expect(parser.parse({
scopeIds: [1, 2, 5, 9]
})).toBe(
expect(
parser.parse(
{
scopeIds: [1, 2, 5, 9],
},
parsingConfigs
)
).toBe(
"from rest_framework import serializers\n\n" +
"class ExampleSerializer(serializers.Serializer):\n" +
" scope_ids = serializers.IntegerField(many=True)\n"
)
})

it('should be able to parse a nested json to drf serializer', () => {
expect(parser.parse({
name: "Kutay",
age: 80,
contactInfo: {
github: "https://github.com/titans55",
address: "World"
}
})).toBe(
expect(
parser.parse(
{
name: "Kutay",
age: 80,
contactInfo: {
github: "https://github.com/titans55",
address: "World"
},
},
parsingConfigs
)
).toBe(
"from rest_framework import serializers\n\n" +
"class ContactInfoSerializer(serializers.Serializer):\n" +
" github = serializers.CharField()\n" +
Expand All @@ -54,37 +79,42 @@ describe('JsonToSerializerParser', () => {


it('should be able to parse a complex json to drf serializer', () => {
expect(parser.parse({
name: "Ashlee Buckner",
age: 27,
eyeColor: "green",
friends: [
{
id: 0,
name: "Gaines Mccall"
},
expect(
parser.parse(
{
id: 1,
name: "Gabrielle Reid"
},
{
id: 2,
name: "Mcguire Macias"
}
],
parent: {
name: "Kathleen Poole",
isActive: true,
age: 36,
eyeColor: "blue",
parent: {
name: "Tillman Ryan",
name: "Ashlee Buckner",
age: 27,
eyeColor: "green",
friends: [
{
id: 0,
name: "Gaines Mccall"
},
{
id: 1,
name: "Gabrielle Reid"
},
{
id: 2,
name: "Mcguire Macias"
}
],
parent: {
age: 32
}
}
},
})).toBe(
name: "Kathleen Poole",
isActive: true,
age: 36,
eyeColor: "blue",
parent: {
name: "Tillman Ryan",
parent: {
age: 32
}
}
},
},
parsingConfigs
)
).toBe(
"from rest_framework import serializers\n\n" +
"class Parent2Serializer(serializers.Serializer):\n" +
" age = serializers.IntegerField()\n\n" +
Expand All @@ -104,7 +134,7 @@ describe('JsonToSerializerParser', () => {
" name = serializers.CharField()\n" +
" age = serializers.IntegerField()\n" +
" eye_color = serializers.CharField()\n" +
" friends = FriendsSerializer(many=True)\n"+
" friends = FriendsSerializer(many=True)\n" +
" parent = ParentSerializer()\n"
)
})
Expand Down
Loading

0 comments on commit 7db33be

Please sign in to comment.