-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic infrastructure for making Application based on JSON domain desc…
…ription
- Loading branch information
Showing
6 changed files
with
126 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/dimau/Uni | ||
|
||
go 1.19 |
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,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) | ||
} |
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,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 | ||
} |
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,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()) | ||
} | ||
} |
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,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 | ||
} |