-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from nikitaksv/dev2
Add formatter package and update README.md
- Loading branch information
Showing
14 changed files
with
349 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,111 @@ | ||
# jgen | ||
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fnikitaksv%2Fjgen.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fnikitaksv%2Fjgen?ref=badge_shield) | ||
# GenData | ||
|
||
[![Godoc Reference](https://godoc.org/github.com/nikitaksv/gendata?status.svg)](http://godoc.org/github.com/nikitaksv/gendata) | ||
![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/nikitaksv/gendata) | ||
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/nikitaksv/gendata/release) | ||
[![codecov](https://codecov.io/gh/nikitaksv/gendata/branch/main/graph/badge.svg?token=7InxmTDBcB)](https://codecov.io/gh/nikitaksv/gendata) | ||
![License](https://img.shields.io/github/license/nikitaksv/gendata) | ||
|
||
--- | ||
|
||
Template Data Generator | ||
|
||
## Installation | ||
|
||
Follow those steps to install the library: | ||
|
||
1. Import the library our code: | ||
|
||
```shell | ||
go get github.com/nikitaksv/gendata | ||
``` | ||
|
||
## Usage | ||
|
||
Example PHP Class generator from JSON data: | ||
```go | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
gendata "github.com/nikitaksv/gendata/pkg/service" | ||
) | ||
|
||
func main() { | ||
tmpl := []byte(` | ||
<?php | ||
namespace common\models; | ||
using yii\base\BaseObject; | ||
/*** | ||
* Class {{ Name }} | ||
* @package common\models | ||
*/ | ||
class {{ Name }} extends BaseObject | ||
{ | ||
{{ Properties }} | ||
/** | ||
* @var {{ Type.Doc }} | ||
*/ | ||
public {{ Type }} ${{ Name.CamelCase }}; | ||
{{ /Properties }} | ||
} | ||
`) | ||
data := []byte(` | ||
{ | ||
"id": 2, | ||
"first_name": "Tam", | ||
"last_name": "Le-Good", | ||
"email": "tlegood1@so-net.ne.jp", | ||
"gender": "Bigender", | ||
"ip_address": "2.92.36.184", | ||
"addresses": [ | ||
{ | ||
"name": "Home", | ||
"city": "Test", | ||
"street": "Test st.", | ||
"house": "1", | ||
"coordinates": { | ||
"lon": 77.2123124, | ||
"lat": 43.2123124 | ||
} | ||
} | ||
], | ||
"skills": [ | ||
"go", | ||
"php" | ||
] | ||
} | ||
`) | ||
|
||
srv := gendata.NewService(nil) | ||
rsp, err := srv.Gen(context.Background(), &gendata.GenRequest{ | ||
Tmpl: tmpl, | ||
Data: data, | ||
Config: &gendata.Config{ | ||
Lang: "php", | ||
DataFormat: "json", | ||
RootClassName: "Gen", | ||
}, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
for _, file := range rsp.RenderedFiles { | ||
buf := new(bytes.Buffer) | ||
_, _ = buf.ReadFrom(file.Content) | ||
fmt.Println("Filename: ", file.FileName) | ||
fmt.Println(buf.String()) | ||
} | ||
} | ||
``` | ||
|
||
Generator JSON5 -> (go strcuts, php class, java class and more) | ||
|
||
|
||
## License | ||
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fnikitaksv%2Fjgen.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fnikitaksv%2Fjgen?ref=badge_large) |
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 was deleted.
Oops, something went wrong.
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,99 @@ | ||
package formatter | ||
|
||
import ( | ||
"github.com/nikitaksv/gendata/pkg/generator/meta" | ||
) | ||
|
||
type ClassNameFormatter func(key meta.Key) string | ||
|
||
type Option func(opts *Options) error | ||
|
||
func WithOptions(opts *Options) Option { | ||
return func(opts_ *Options) error { | ||
opts_.RootClassName = opts.RootClassName | ||
opts_.PrefixClassName = opts.PrefixClassName | ||
opts_.SuffixClassName = opts.SuffixClassName | ||
opts_.SortProperties = opts.SortProperties | ||
opts_.TypeFormatters = opts.TypeFormatters | ||
opts_.ClassNameFormatter = opts.ClassNameFormatter | ||
return nil | ||
} | ||
} | ||
|
||
type Options struct { | ||
RootClassName string `json:"rootClassName"` | ||
PrefixClassName string `json:"prefixClassName"` | ||
SuffixClassName string `json:"suffixClassName"` | ||
SortProperties bool `json:"sortProperties"` | ||
TypeFormatters *meta.TypeFormatters `json:"typeFormatters"` | ||
ClassNameFormatter ClassNameFormatter `json:"classNameFormatter"` | ||
} | ||
|
||
func (o *Options) apply(opts ...Option) error { | ||
for _, opt := range opts { | ||
if err := opt(o); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type Formatter interface { | ||
Format(m *meta.Meta, opts ...Option) (*meta.Meta, error) | ||
} | ||
|
||
func NewFormatter() Formatter { | ||
return &formatter{} | ||
} | ||
|
||
type formatter struct{} | ||
|
||
func (f formatter) Format(m *meta.Meta, opts ...Option) (*meta.Meta, error) { | ||
options := &Options{} | ||
if err := options.apply(opts...); err != nil { | ||
return nil, err | ||
} | ||
|
||
if options.SortProperties { | ||
m.Sort() | ||
} | ||
|
||
if m.Key.String() == "" { | ||
m.Key = meta.Key(options.RootClassName) | ||
} | ||
|
||
m.Key = meta.Key(f.className(m.Key, options)) | ||
m.Type.Key = m.Key | ||
m.Type.Formatters = options.TypeFormatters | ||
for _, property := range m.Properties { | ||
property.Type.Formatters = options.TypeFormatters | ||
|
||
if property.Type.IsObject() || property.Type.Value == meta.TypeArrayObject { | ||
property.Key = meta.Key(f.className(property.Key, options)) | ||
property.Type.Key = property.Key | ||
} | ||
if property.Nest != nil { | ||
var err error | ||
property.Nest, err = f.Format(property.Nest, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
func (f formatter) className(name meta.Key, options *Options) string { | ||
className := "" | ||
if options.PrefixClassName != "" { | ||
className = options.PrefixClassName + name.PascalCase() + options.SuffixClassName | ||
} else { | ||
className = name.String() + options.SuffixClassName | ||
} | ||
if options.ClassNameFormatter != nil { | ||
className = options.ClassNameFormatter(meta.Key(className)) | ||
} | ||
|
||
return className | ||
} |
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
Oops, something went wrong.