Skip to content

Commit

Permalink
Merge pull request #4 from nikitaksv/dev2
Browse files Browse the repository at this point in the history
Add formatter package and update README.md
  • Loading branch information
nikitaksv authored Mar 27, 2022
2 parents 9d23fe9 + 985e197 commit a279d18
Show file tree
Hide file tree
Showing 14 changed files with 349 additions and 140 deletions.
113 changes: 108 additions & 5 deletions README.md
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)
2 changes: 1 addition & 1 deletion cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"strings"

"github.com/nikitaksv/gendata/internal/service"
"github.com/nikitaksv/gendata/pkg/service"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"go.uber.org/zap"
Expand Down
42 changes: 0 additions & 42 deletions internal/generator/parser/parser.go

This file was deleted.

99 changes: 99 additions & 0 deletions pkg/generator/formatter/formatter.go
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"html/template"
"io"

"github.com/nikitaksv/gendata/internal/generator/meta"
"github.com/nikitaksv/gendata/internal/syntax"
"github.com/nikitaksv/gendata/pkg/generator/meta"
"github.com/nikitaksv/gendata/pkg/syntax"
"github.com/pkg/errors"
)

Expand Down
33 changes: 13 additions & 20 deletions internal/generator/meta/meta.go → pkg/generator/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,16 @@ func (k Key) DotCase() string {
}

type Type struct {
Key Key `json:"key"`
Value string `json:"value"`

formatters *TypeFormatters
Key Key `json:"key"`
Value string `json:"value"`
Formatters *TypeFormatters `json:"formatters"`
}

func (t Type) String() string {
return t.formatters.Type(t)
return t.Formatters.Type(t)
}
func (t Type) Doc() string {
return t.formatters.Doc(t)
return t.Formatters.Doc(t)
}
func (t Type) IsNull() bool {
return t.Value == TypeNull
Expand All @@ -131,19 +130,16 @@ func (t Type) IsObject() bool {
return t.Value == TypeObject
}

func TypeOf(key Key, v interface{}, f *TypeFormatters) Type {
t := Type{
Key: key,
formatters: f,
}
func TypeOf(key Key, v interface{}) Type {
t := Type{Key: key}
switch vType := v.(type) {
case *dynjson.Object:
t.Value = TypeObject
return t
case *dynjson.Array:
return typeOfArray(key, vType.Elements, f)
return typeOfArray(key, vType.Elements)
case []interface{}:
return typeOfArray(key, vType, f)
return typeOfArray(key, vType)
case map[string]interface{}:
t.Value = TypeObject
return t
Expand All @@ -168,11 +164,8 @@ func TypeOf(key Key, v interface{}, f *TypeFormatters) Type {
}
}

func typeOfArray(key Key, arr []interface{}, f *TypeFormatters) Type {
t := Type{
Key: key,
formatters: f,
}
func typeOfArray(key Key, arr []interface{}) Type {
t := Type{Key: key}

mx := map[string]int{
TypeArrayBool: 0,
Expand All @@ -188,11 +181,11 @@ func typeOfArray(key Key, arr []interface{}, f *TypeFormatters) Type {
case *dynjson.Object:
mx[TypeArrayObject]++
case *dynjson.Array:
mx[typeOfArray(key, vType.Elements, f).Value]++
mx[typeOfArray(key, vType.Elements).Value]++
case map[string]interface{}:
mx[TypeArrayObject]++
case []interface{}:
mx[typeOfArray(key, vType, f).Value]++
mx[typeOfArray(key, vType).Value]++
case int, int8, int16, int32, int64:
mx[TypeArrayInt]++
case float32, float64:
Expand Down
Loading

0 comments on commit a279d18

Please sign in to comment.