Skip to content

Commit

Permalink
Add support for string types
Browse files Browse the repository at this point in the history
Signed-off-by: Saswata Mukherjee <[email protected]>
  • Loading branch information
saswatamcode committed Aug 1, 2021
1 parent dc8cc39 commit 48335e8
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions pkg/yamlgen/yamlgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"context"
"fmt"
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"

"github.com/dave/jennifer/jen"
"github.com/pkg/errors"
Expand Down Expand Up @@ -56,7 +56,11 @@ func GenGoCode(src []byte) (string, error) {
if typeDecl, ok := genericDecl.Specs[0].(*ast.TypeSpec); ok {
var structFields []jen.Code
// Cast to `type struct`.
structDecl := typeDecl.Type.(*ast.StructType)
structDecl, ok := typeDecl.Type.(*ast.StructType)
if !ok {
generatedCode.Type().Id(typeDecl.Name.Name).Id(string(src[typeDecl.Type.Pos()-1 : typeDecl.Type.End()-1]))
continue
}
fields := structDecl.Fields.List
arrayInit := make(jen.Dict)

Expand All @@ -68,20 +72,12 @@ func GenGoCode(src []byte) (string, error) {
if n.IsExported() {
pos := n.Obj.Decl.(*ast.Field)

// Make type map to check if field is array.
info := types.Info{Types: make(map[ast.Expr]types.TypeAndValue)}
_, err = (&types.Config{Importer: importer.ForCompiler(fset, "source", nil)}).Check("mypkg", fset, []*ast.File{f}, &info)
if err != nil {
return "", err
}
typ := info.Types[field.Type].Type

switch typ.(type) {
case *types.Slice:
// Field is of type array so initialize it using code like `[]Type{Type{}}`.
// Check if field is a slice type.
sliceRe := regexp.MustCompile(`.*\[.*\].*`)
if sliceRe.MatchString(types.ExprString(field.Type)) {
arrayInit[jen.Id(n.Name)] = jen.Id(string(src[pos.Type.Pos()-1 : pos.Type.End()-1])).Values(jen.Id(string(src[pos.Type.Pos()+1 : pos.Type.End()-1])).Values())
default:
}

// Copy struct field to generated code.
if pos.Tag != nil {
structFields = append(structFields, jen.Id(n.Name).Id(string(src[pos.Type.Pos()-1:pos.Type.End()-1])).Id(pos.Tag.Value))
Expand Down

0 comments on commit 48335e8

Please sign in to comment.