Skip to content

Commit

Permalink
Basic infrastructure for making Application based on JSON domain desc…
Browse files Browse the repository at this point in the history
…ription
  • Loading branch information
dimau committed Jan 6, 2023
1 parent 65b0d81 commit f263a2d
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Resut of compilations
resultApplication

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/dimau/Uni

go 1.19
35 changes: 35 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"github.com/dimau/Uni/pkg/appSourceCodeCreator"
"github.com/dimau/Uni/pkg/domainDescriptionParser"
"io/ioutil"
"log"
"os"
)

func main() {
// Read marshalled text of JSON Domain Description from the file
file, err := os.Open("./resultApplication/textDomainDescription.json")
if err != nil {
log.Fatalln("Can't open text file with JSON domain description " + err.Error())
}

// Closing file after all read operations done
defer func() {
if err := file.Close(); err != nil {
log.Fatalln(err.Error())
}
}()

// Reading file with domain description
data, err := ioutil.ReadAll(file)
if err != nil {
log.Fatalln("Can't read from text file with JSON domain description " + err.Error())
}

// Handle JSON domain description and make a source code for an Application
jsonDomainDescription := domainDescriptionParser.ParseJsonDomainDescription(data)
sourceFileContent := appSourceCodeCreator.CreateAppSourceCode(jsonDomainDescription)
appSourceCodeCreator.PrintAppSourceCodeToFile(sourceFileContent)
}
20 changes: 20 additions & 0 deletions pkg/appSourceCodeCreator/createAppSourceCode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package appSourceCodeCreator

import (
"fmt"
"github.com/dimau/Uni/pkg/domainDescriptionParser"
)

func CreateAppSourceCode(jsonDomainDescription *domainDescriptionParser.Domain) *[]byte {
sourceFileContent := []byte("package main\n\n")

for _, class := range jsonDomainDescription.Classes {
sourceFileContent = append(sourceFileContent, []byte(fmt.Sprintf("type %v struct {\n", class.Name))...)
for _, attribute := range class.Attributes {
sourceFileContent = append(sourceFileContent, []byte(fmt.Sprintf(" %v %v\n", attribute.Name, attribute.ValueType.Name))...)
}
sourceFileContent = append(sourceFileContent, []byte(fmt.Sprintf("}\n\n"))...)
}

return &sourceFileContent
}
27 changes: 27 additions & 0 deletions pkg/appSourceCodeCreator/printAppSourceCodeToFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package appSourceCodeCreator

import (
"log"
"os"
)

func PrintAppSourceCodeToFile(sourceFileContent *[]byte) {
// Create or (if already created) clean current file and open for writing
file, err := os.Create("./resultApplication/application.go")
if err != nil {
log.Fatalln(err.Error())
}

// Closing file after all writes operations done
defer func() {
if err := file.Close(); err != nil {
log.Fatalln(err.Error())
}
}()

// Writes all bytes to text file with source code for the App
_, err = file.Write(*sourceFileContent)
if err != nil {
log.Fatalln(err.Error())
}
}
38 changes: 38 additions & 0 deletions pkg/domainDescriptionParser/domainDescriptionParser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package domainDescriptionParser

import (
"encoding/json"
"log"
)

type ValueType struct {
Id string `json:"id"`
Name string `json:"name"`
}

type Attribute struct {
Id string `json:"id"`
Name string `json:"name"`
Database string `json:"database"`
ValueType ValueType `json:"value-type"`
}

type Class struct {
Id string `json:"id"`
Name string `json:"name"`
Database string `json:"database"`
Attributes []Attribute `json:"attributes"`
}

type Domain struct {
Classes []Class `json:"classes"`
}

func ParseJsonDomainDescription(domain []byte) *Domain {
var data = &Domain{}
err := json.Unmarshal(domain, data)
if err != nil {
log.Fatalln("Error unmarshalling", err.Error())
}
return data
}

0 comments on commit f263a2d

Please sign in to comment.