Browse Source

Added basic structure creation

  * Added PlacePackageName() to place the leading line in the go file.
  * Added allow building imports that are known to be needed.
  * Added fields, you can now build a structure via go code.
  * Added an example building `example.go` ExamplePerson structure.
Apollo 2 years ago
parent
commit
f0a3a62ba2
6 changed files with 238 additions and 0 deletions
  1. 147 0
      autostruct.go
  2. 12 0
      examples/example.go
  3. 7 0
      examples/go.mod
  4. 30 0
      examples/main.go
  5. 31 0
      sample.json
  6. 11 0
      utils.go

+ 147 - 0
autostruct.go

@@ -0,0 +1,147 @@
+package autostruct
+
+import (
+	"fmt"
+	"os"
+)
+
+// Generates structures given json
+//
+// Can also be used to generate structures via go code
+type AutoStruct struct {
+	StructName  string   // Name to prefix to all structures generated
+	PackageName string   // Package to assign the new go file
+	FileName    string   // The name to assign the new go file
+	File        *os.File // Access to the new go file (writer)
+}
+
+//
+func (a *AutoStruct) Init(fname, packname, structname string) error {
+	a.StructName = structname
+	a.FileName = fname
+	a.PackageName = packname
+	if !FileExists(a.FileName + ".go") {
+		var err error
+		a.File, err = os.Create(a.FileName + ".go")
+		if err != nil {
+			return err
+		}
+	} else {
+		return fmt.Errorf("file already exists, update mode is not yet implemented")
+	}
+	return nil
+}
+
+// Places the package name defined in the init
+func (a *AutoStruct) PlacePackageName() {
+	a.File.Write([]byte("package " + a.PackageName + "\n"))
+}
+
+// In cases where you use a specific type from another module
+//
+// Don't forget to call EndImports() to close this, go will be cranky
+func (a *AutoStruct) StartImports() {
+	a.File.Write([]byte("import (\n"))
+}
+
+// Adds a unambiguous import
+//
+// Use AddAliasImport() for 'alias "import"'
+func (a *AutoStruct) AddImport(importname string) {
+	a.File.Write([]byte("\t\"" + importname + "\"\n"))
+}
+
+// Adds an aliased import
+//
+// Ex  door "red-gree.com/door"
+//
+// Use AddImport() for a unambiguous import
+func (a *AutoStruct) AddAliasImport(importalias, importname string) {
+	a.File.Write([]byte("\t" + importalias + " \"" + importname + "\"\n"))
+}
+
+// Closes the import statement
+func (a *AutoStruct) EndImports() {
+	a.File.Write([]byte(")\n"))
+}
+
+// Begins a structure
+func (a *AutoStruct) StartStruct(structname string) {
+	a.File.Write([]byte("type " + a.StructName + structname + " struct {\n"))
+}
+
+// Adds a field to the structure
+//
+// Ex   Name string
+//
+// Use AddCommentField() to add a comment with the field
+//
+// Use AddJsonField() to add a json tag with the field
+//
+// Use AddJsonCommentField() to add a json tag and a comment
+func (a *AutoStruct) AddField(fieldname, fieldtype string) {
+	a.File.Write([]byte("\t" + fieldname + " " + fieldtype + "\n"))
+}
+
+// Adds a field to the structure (includes a comment about the field)
+//
+// Ex    Name string // The BBS user name
+//
+// Use AddField() to add a field without a comment or json tag
+//
+// Use AddJsonCommentField() to add a field with a comment and a json tag
+//
+// Use AddJsonField() to add a field with a json tag but no comment
+func (a *AutoStruct) AddCommentField(fieldname, fieldtype, comment string) {
+	a.File.Write([]byte("\t" + fieldname + " " + fieldtype + " // " + comment + "\n"))
+}
+
+// Adds a field to the structure (includes a json tag)
+//
+// Ex     Name string `json:"name"`
+//
+// Use json tag "-" to mark fields to not be in json's marshal or unmarshal process
+//
+// Use AddField() to add a field without a comment or json tag
+//
+// Use AddCommentField() to add a field without a json tag but with a comment
+//
+// Use AddJsonCommentField() to add a field with a json tag and a comment
+func (a *AutoStruct) AddJsonField(fieldname, fieldtype, jsonname string) {
+	a.File.Write([]byte("\t" + fieldname + " " + fieldtype + " `json:\"" + jsonname + "\"`\n"))
+}
+
+// Adds a field to the structure (includes both a json tag and a comment)
+//
+// Ex     Name string `json:"name"` // The BBS user name
+//
+// Use json tag "-" to mark fields to not be in json's marshal or unmarshal process
+//
+// Use AddJsonField() to add a field with a json tag but no comment
+//
+// Use AddField() to add a field without a comment or json tag
+//
+// Use AddCommentField() to add a field with a comment but no json tag
+func (a *AutoStruct) AddJsonCommentField(fieldname, fieldtype, jsonname, comment string) {
+	a.File.Write([]byte("\t" + fieldname + " " + fieldtype + " `json:\"" + jsonname + "\"` // " + comment + "\n"))
+}
+
+// Ends/Closes the structure
+func (a *AutoStruct) EndStruct() {
+	a.File.Write([]byte("}\n"))
+}
+
+// Adds some space between things
+func (a *AutoStruct) Newline() {
+	a.File.Write([]byte("\n"))
+}
+
+// Adds a single line comment
+func (a *AutoStruct) Comment(comment string) {
+	a.File.Write([]byte("// " + comment + "\n"))
+}
+
+// Closes the newly formed go file
+func (a *AutoStruct) Close() {
+	a.File.Close()
+}

+ 12 - 0
examples/example.go

@@ -0,0 +1,12 @@
+package main
+
+import (
+	"time"
+)
+
+type ExamplePerson struct {
+	Name string `json:"name"` // Person's name
+	Age time.Time `json:"age"` // Person's age, saved and loaded as unix
+	Weight float32 `json:"weight"` // Person's weight in ounces
+	Height float32 `json:"height"` // Person's height in inches
+}

+ 7 - 0
examples/go.mod

@@ -0,0 +1,7 @@
+module red-green.com/autostruct-examples
+
+go 1.18
+
+replace red-green.com/autostruct => ../
+
+require red-green.com/autostruct v0.0.0-00010101000000-000000000000

+ 30 - 0
examples/main.go

@@ -0,0 +1,30 @@
+package main
+
+import (
+	"fmt"
+
+	"red-green.com/autostruct"
+)
+
+func main() {
+	var a autostruct.AutoStruct
+	var err error
+	err = a.Init("example", "main", "Example")
+	if err != nil {
+		fmt.Println(err)
+	} else {
+		defer a.Close()
+		a.PlacePackageName()
+		a.Newline()
+		a.StartImports()
+		a.AddImport("time")
+		a.EndImports()
+		a.Newline()
+		a.StartStruct("Person")
+		a.AddJsonCommentField("Name", "string", "name", "Person's name")
+		a.AddJsonCommentField("Age", "time.Time", "age", "Person's age, saved and loaded as unix")
+		a.AddJsonCommentField("Weight", "float32", "weight", "Person's weight in ounces")
+		a.AddJsonCommentField("Height", "float32", "height", "Person's height in inches")
+		a.EndStruct()
+	}
+}

+ 31 - 0
sample.json

@@ -0,0 +1,31 @@
+{
+    "name": "AutoStruct Sample JSON",
+    "version": 1.0,
+    "favorites": {
+        "foods": [
+            "tacos",
+            "burritos",
+            "hotdogs",
+            "sandwiches"
+        ],
+        "animals": [
+            "squirrels",
+            "dogs",
+            "birds",
+            "tigers",
+            "lions",
+            "bears"
+        ],
+        "numbers": [
+            0,
+            2,
+            3,
+            5,
+            6,
+            9
+        ]
+    },
+    "contributors": [
+        "apollo"
+    ]
+}

+ 11 - 0
utils.go

@@ -0,0 +1,11 @@
+package autostruct
+
+import (
+	"errors"
+	"os"
+)
+
+func FileExists(path string) bool {
+	_, err := os.Stat(path)
+	return !errors.Is(err, os.ErrNotExist)
+}