Skip to content

Commit

Permalink
all: provided constant declaration values.
Browse files Browse the repository at this point in the history
  • Loading branch information
kucjac committed Nov 22, 2021
1 parent 2c459ff commit 85ed552
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 12 deletions.
2 changes: 1 addition & 1 deletion parser/load-packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (r *rootPackage) parseTypePkg(initWg, fg *sync.WaitGroup) {
if !ok {
continue
}
if err := p.NewConstant(ot.Name(), declType); err != nil {
if err := p.NewConstant(ot.Name(), declType, ot.Val()); err != nil {
if r.loadConfig.Verbose {
fmt.Println(err)
}
Expand Down
18 changes: 18 additions & 0 deletions parser/parse_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"go/constant"
"testing"

"github.com/kucjac/gentools/types"
Expand Down Expand Up @@ -174,6 +175,23 @@ func TestParse(t *testing.T) {
if enumOne.Comment != "EnumeratedOne defines a first enumerated type value.\n" {
t.Fatalf("EnumeratedOne comment doesn't match: '%s'", enumOne.Comment)
}

if !enumOne.Constant {
t.Fatal("EnumeratedOne should be a constant")
}

if enumOne.Val == nil {
t.Fatal("EnumeratedOne constant value should be defined")
}

if enumOne.Val.Kind() != constant.Int || enumOne.Val.String() != "1" {
t.Fatal("EnumeratedOne constant value should be of kind Integer and take value of 1")
}

v, ok := enumOne.ConstValue().(int)
if !ok || v != int(1) {
t.Fatalf("EnumeratedOne constant value doesn't match: %v", enumOne.ConstValue())
}
})

fooID, ok := pkg.GetType("FooID")
Expand Down
62 changes: 62 additions & 0 deletions types/declaration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package types

import (
"fmt"
"go/constant"
"strconv"
)

// Declaration is the variable or constant declaration.
type Declaration struct {
Comment string
Name string
Type Type
Constant bool
Val constant.Value
Package *Package
}

// ConstValue gets the basic value of given constant declaration type.
// The method panics if the Declaration is not a constant but variable.
// For selected field kind it returns following Value Types:
// - KindString - string
// - KindBool - bool
// - KindInt, KindInt8, KindInt16, KindInt32, KindInt64: - int
// - KindUint, KindUint8, KindUint16, KindUint32, KindUint64: - uint
// - KindFloat64, KindFloat32: - float64
// - KindComplex64, KindComplex128: - complex128
func (d Declaration) ConstValue() interface{} {
if !d.Constant {
panic(fmt.Sprintf("declaration is not a constant: %s", d))
}

switch d.Type.Kind() {
case KindString:
return d.Val.String()
case KindBool:
v, _ := strconv.ParseBool(d.Val.String())
return v
case KindInt, KindInt8, KindInt16, KindInt32, KindInt64:
i, _ := strconv.ParseInt(d.Val.String(), 10, 64)
return int(i)
case KindUint, KindUint8, KindUint16, KindUint32, KindUint64:
u, _ := strconv.ParseUint(d.Val.String(), 10, 64)
return uint(u)
case KindFloat64, KindFloat32:
v, _ := strconv.ParseFloat(d.Val.String(), 64)
return v
case KindComplex64, KindComplex128:
v, _ := strconv.ParseComplex(d.Val.String(), 128)
return v
default:
panic(fmt.Sprintf("Uknown constant type Kind: %s", d.Type.Kind()))
}
}

// String provides a string visual form of the declaration.
func (d Declaration) String() string {
if !d.Constant {
return fmt.Sprintf("var %s.%s", d.Package.Identifier, d.Name)
}
return fmt.Sprintf("const %s.%s(%s)", d.Package.Identifier, d.Name, d.Val.String())
}
26 changes: 15 additions & 11 deletions types/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"fmt"
"go/constant"
"strings"
"sync"
)
Expand Down Expand Up @@ -30,16 +31,27 @@ func (p *Package) NewVariable(name string, tp Type) error {
if err := p.hasName(name); err != nil {
return err
}
p.Declarations[name] = Declaration{Name: name, Type: tp}
p.Declarations[name] = Declaration{
Name: name,
Type: tp,
Constant: false,
Package: p,
}
return nil
}

// NewConstant adds new package constant Declaration.
func (p *Package) NewConstant(name string, tp Type) error {
func (p *Package) NewConstant(name string, tp Type, value constant.Value) error {
if err := p.hasName(name); err != nil {
return err
}
p.Declarations[name] = Declaration{Name: name, Type: tp, Constant: true}
p.Declarations[name] = Declaration{
Name: name,
Type: tp,
Constant: true,
Val: value,
Package: p,
}
return nil
}

Expand Down Expand Up @@ -225,11 +237,3 @@ func trimZeroRuneSpace(typeOf string) string {
}
return typeOf
}

// Declaration is the variable or constant declaration.
type Declaration struct {
Comment string
Name string
Type Type
Constant bool
}

0 comments on commit 85ed552

Please sign in to comment.