Skip to content

Commit

Permalink
Merge pull request #984 from Workiva/release_2_16_0
Browse files Browse the repository at this point in the history
INFENG-5221 Release 2.16.0
  • Loading branch information
brianshannan-wf authored Mar 22, 2018
2 parents b2c030e + 0473e89 commit 0689625
Show file tree
Hide file tree
Showing 157 changed files with 957 additions and 348 deletions.
9 changes: 7 additions & 2 deletions compiler/generator/golang/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ func (g *Generator) GenerateConstantsContents(constants []*parser.Constant) erro
return nil
}

// quote creates a Go string literal for a string.
func (g *Generator) quote(s string) string {
return strconv.Quote(s);
}

// generateConstantValue recursively generates the string representation of
// a, possibly complex, constant value.
func (g *Generator) generateConstantValue(t *parser.Type, value interface{}) string {
Expand Down Expand Up @@ -239,7 +244,7 @@ func (g *Generator) generateConstantValue(t *parser.Type, value interface{}) str
case "bool", "i8", "byte", "i16", "i32", "i64", "double":
return fmt.Sprintf("%v", value)
case "string":
return fmt.Sprintf("%s", strconv.Quote(value.(string)))
return g.quote(value.(string))
case "binary":
return fmt.Sprintf("[]byte(\"%s\")", value)
case "list":
Expand Down Expand Up @@ -1856,7 +1861,7 @@ func (g *Generator) generateProcessor(service *parser.Service) string {
if len(method.Annotations) > 0 {
contents += fmt.Sprintf("\tp.AddToAnnotationsMap(\"%s\", map[string]string{\n", methodLower)
for _, annotation := range method.Annotations {
contents += fmt.Sprintf("\t\t\"%s\": \"%s\",\n", annotation.Name, annotation.Value)
contents += fmt.Sprintf("\t\t\"%s\": %s,\n", annotation.Name, g.quote(annotation.Value))
}
contents += "\t})\n"
}
Expand Down
24 changes: 18 additions & 6 deletions compiler/generator/java/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ func (g *Generator) generateEnumConstFromValue(t *parser.Type, value int) string
panic("value not found")
}

// quote creates a Java string literal for a string.
func (g *Generator) quote(s string) string {
// For now, just use Go quoting rules.
return strconv.Quote(s);
}

func (g *Generator) generateConstantValueRec(t *parser.Type, value interface{}) (string, string) {
underlyingType := g.Frugal.UnderlyingType(t)

Expand Down Expand Up @@ -331,7 +337,7 @@ func (g *Generator) generateConstantValueRec(t *parser.Type, value interface{})
case "double":
return "", fmt.Sprintf("%v", value)
case "string":
return "", fmt.Sprintf("%v", strconv.Quote(value.(string)))
return "", g.quote(value.(string))
case "binary":
return "", fmt.Sprintf("java.nio.ByteBuffer.wrap(\"%v\".getBytes())", value)
}
Expand Down Expand Up @@ -916,12 +922,18 @@ func (g *Generator) generateInstanceVars(s *parser.Struct) string {
if field.Comment != nil {
contents += g.GenerateBlockComment(field.Comment, tab)
}
modifier := "required"
if field.Modifier == parser.Optional {
modifier := ""
if field.Modifier == parser.Required {
modifier = "required"
} else if field.Modifier == parser.Optional {
modifier = "optional"
}
contents += fmt.Sprintf(tab+"public %s %s; // %s\n",
g.getJavaTypeFromThriftType(field.Type), field.Name, modifier)
modifierComment := ""
if modifier != "" {
modifierComment = " // " + modifier
}
contents += fmt.Sprintf(tab+"public %s %s;%s\n",
g.getJavaTypeFromThriftType(field.Type), field.Name, modifierComment)
}
return contents
}
Expand Down Expand Up @@ -2965,7 +2977,7 @@ func (g *Generator) generateServer(service *parser.Service) string {
if len(method.Annotations) > 0 {
contents += tabtabtab + fmt.Sprintf("java.util.Map<String, String> %sMap = new java.util.HashMap<>();\n", method.Name)
for _, annotation := range method.Annotations {
contents += tabtabtab + fmt.Sprintf("%sMap.put(\"%s\", \"%s\");\n", method.Name, annotation.Name, annotation.Value)
contents += tabtabtab + fmt.Sprintf("%sMap.put(\"%s\", %s);\n", method.Name, annotation.Name, g.quote(annotation.Value))
}
contents += tabtabtab + fmt.Sprintf("annotationsMap.put(\"%s\", %sMap);\n", parser.LowercaseFirstLetter(method.Name), method.Name)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/generator/python/asyncio.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (g *AsyncIOGenerator) generateProcessor(service *parser.Service) string {
if len(method.Annotations) > 0 {
annotations := make([]string, len(method.Annotations))
for i, annotation := range method.Annotations {
annotations[i] = fmt.Sprintf("'%s': '%s'", annotation.Name, annotation.Value)
annotations[i] = fmt.Sprintf("%s: %s", g.quote(annotation.Name), g.quote(annotation.Value))
}
contents += tabtab +
fmt.Sprintf("self.add_to_annotations_map('%s', {%s})\n", methodLower, strings.Join(annotations, ", "))
Expand Down
90 changes: 79 additions & 11 deletions compiler/generator/python/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package python
import (
"fmt"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"

Expand All @@ -43,16 +45,23 @@ const (
asyncio
)

// genInfo tracks file generation inputs for later __init__.py imports
type genInfo struct {
fileName, frugalName string
fileType generator.FileType
}

// Generator implements the LanguageGenerator interface for Python.
type Generator struct {
*generator.BaseGenerator
outputDir string
typesFile *os.File
history map[string][]genInfo
}

// NewGenerator creates a new Python LanguageGenerator.
func NewGenerator(options map[string]string) generator.LanguageGenerator {
gen := &Generator{&generator.BaseGenerator{Options: options}, "", nil}
gen := &Generator{&generator.BaseGenerator{Options: options}, "", nil, map[string][]genInfo{}}
switch getAsyncOpt(options) {
case tornado:
return &TornadoGenerator{gen}
Expand Down Expand Up @@ -101,9 +110,51 @@ func (g *Generator) SetupGenerator(outputDir string) error {

// TeardownGenerator is run after generation.
func (g *Generator) TeardownGenerator() error {
if err := g.generateInitFile(); err != nil {
return err
}

return g.typesFile.Close()
}

// generateInit adds subpackage imports to __init__.py files
// to simplify consumer import paths
func (g *Generator) generateInitFile() error {
initFile, err := os.OpenFile(path.Join(g.outputDir, "__init__.py"), os.O_WRONLY, 0644)
if err != nil {
return err
}
defer initFile.Close()

imports := []string{}
if fileInfoSlice, ok := g.history[g.outputDir]; ok {
for _, fileInfo := range fileInfoSlice {
switch fileInfo.fileType {
case generator.PublishFile:
imports = append(imports, fmt.Sprintf("from .%s import %sPublisher", fileInfo.fileName, fileInfo.frugalName))
case generator.SubscribeFile:
imports = append(imports, fmt.Sprintf("from .%s import %sSubscriber", fileInfo.fileName, fileInfo.frugalName))
case generator.CombinedServiceFile:
imports = append(imports,
fmt.Sprintf("from .%s import Iface as F%sIface", fileInfo.fileName, fileInfo.frugalName))
imports = append(imports,
fmt.Sprintf("from .%s import Client as F%sClient", fileInfo.fileName, fileInfo.frugalName))
case generator.ObjectFile:
if fileInfo.frugalName == "ttypes" {
imports = append(imports, "from .ttypes import *")
}
}
}
}

sort.Strings(imports)
if _, err := initFile.WriteString(strings.Join(imports, "\n") + "\n"); err != nil {
return err
}

return nil
}

// GenerateConstantsContents generates constants.
func (g *Generator) GenerateConstantsContents(constants []*parser.Constant) error {
file, err := g.GenerateFile("constants", g.outputDir, generator.ObjectFile)
Expand Down Expand Up @@ -135,6 +186,12 @@ func (g *Generator) GenerateConstantsContents(constants []*parser.Constant) erro
return err
}

// quote creates a Python string literal for a string.
func (g *Generator) quote(s string) string {
// For now, just use Go quoting rules.
return strconv.Quote(s);
}

func (g *Generator) generateConstantValue(t *parser.Type, value interface{}, ind string) (parser.IdentifierType, string) {
if value == nil {
return parser.NonIdentifier, "None"
Expand Down Expand Up @@ -171,7 +228,7 @@ func (g *Generator) generateConstantValue(t *parser.Type, value interface{}, ind
case "i8", "byte", "i16", "i32", "i64", "double":
return parser.NonIdentifier, fmt.Sprintf("%v", value)
case "string", "binary":
return parser.NonIdentifier, fmt.Sprintf("%s", strconv.Quote(value.(string)))
return parser.NonIdentifier, g.quote(value.(string))
case "list", "set":
contents := ""
if underlyingType.Name == "set" {
Expand Down Expand Up @@ -237,7 +294,7 @@ func (g *Generator) GenerateEnum(enum *parser.Enum) error {
comment := append([]string{}, enum.Comment...)
for _, value := range enum.Values {
if value.Comment != nil {
comment = append(append(comment, value.Name + ": " + value.Comment[0]), value.Comment[1:]...)
comment = append(append(comment, value.Name+": "+value.Comment[0]), value.Comment[1:]...)
}
}
if len(comment) != 0 {
Expand Down Expand Up @@ -306,7 +363,7 @@ func (g *Generator) generateStruct(s *parser.Struct) string {
contents += g.generateClassDocstring(s)

contents += g.generateDefaultMarkers(s)
contents += g.generateInit(s)
contents += g.generateInitMethod(s)

contents += g.generateRead(s)
contents += g.generateWrite(s)
Expand Down Expand Up @@ -347,8 +404,8 @@ func (g *Generator) generateDefaultMarkers(s *parser.Struct) string {
return contents
}

// generateInit generates the init method for a class.
func (g *Generator) generateInit(s *parser.Struct) string {
// generateInitMethod generates the init method for a class.
func (g *Generator) generateInitMethod(s *parser.Struct) string {
if len(s.Fields) == 0 {
return ""
}
Expand Down Expand Up @@ -696,18 +753,29 @@ func (g *Generator) GenerateDependencies(dir string) error {

// GenerateFile generates the given FileType.
func (g *Generator) GenerateFile(name, outputDir string, fileType generator.FileType) (*os.File, error) {
var fileName string

switch fileType {
case generator.PublishFile:
return g.CreateFile(fmt.Sprintf("f_%s_publisher", name), outputDir, lang, false)
fileName = fmt.Sprintf("f_%s_publisher", name)
case generator.SubscribeFile:
return g.CreateFile(fmt.Sprintf("f_%s_subscriber", name), outputDir, lang, false)
fileName = fmt.Sprintf("f_%s_subscriber", name)
case generator.CombinedServiceFile:
return g.CreateFile(fmt.Sprintf("f_%s", name), outputDir, lang, false)
fileName = fmt.Sprintf("f_%s", name)
case generator.ObjectFile:
return g.CreateFile(fmt.Sprintf("%s", name), outputDir, lang, false)
fileName = fmt.Sprintf("%s", name)
default:
return nil, fmt.Errorf("Bad file type for Python generator: %s", fileType)
}

// No subscriber implementation for vanilla Python, so we need to omit that
if !(getAsyncOpt(g.Options) == synchronous && fileType == generator.SubscribeFile) {
// Track history of generated file input for reference later
// to add imports in __init__.py files
g.history[outputDir] = append(g.history[outputDir], genInfo{fileName, name, fileType})
}

return g.CreateFile(fileName, outputDir, lang, false)
}

// GenerateDocStringComment generates the autogenerated notice.
Expand Down Expand Up @@ -1220,7 +1288,7 @@ func (g *Generator) generateProcessor(service *parser.Service) string {
if len(method.Annotations) > 0 {
annotations := make([]string, len(method.Annotations))
for i, annotation := range method.Annotations {
annotations[i] = fmt.Sprintf("'%s': '%s'", annotation.Name, annotation.Value)
annotations[i] = fmt.Sprintf("'%s': %s", annotation.Name, g.quote(annotation.Value))
}
contents += tabtab +
fmt.Sprintf("self.add_to_annotations_map('%s', {%s})\n", methodLower, strings.Join(annotations, ", "))
Expand Down
2 changes: 1 addition & 1 deletion compiler/globals/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// Version of the Frugal compiler.
const Version = "2.15.1"
const Version = "2.16.0"

// Global variables.
var (
Expand Down
2 changes: 1 addition & 1 deletion examples/dart/gen-dart/v1_music/lib/src/f_album.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated by Frugal Compiler (2.15.1)
// Autogenerated by Frugal Compiler (2.16.0)
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING

import 'dart:typed_data' show Uint8List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated by Frugal Compiler (2.15.1)
// Autogenerated by Frugal Compiler (2.16.0)
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated by Frugal Compiler (2.15.1)
// Autogenerated by Frugal Compiler (2.16.0)
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING

class PerfRightsOrg {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated by Frugal Compiler (2.15.1)
// Autogenerated by Frugal Compiler (2.16.0)
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING

import 'dart:typed_data' show Uint8List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated by Frugal Compiler (2.15.1)
// Autogenerated by Frugal Compiler (2.16.0)
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING


Expand Down
2 changes: 1 addition & 1 deletion examples/dart/gen-dart/v1_music/lib/src/f_track.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated by Frugal Compiler (2.15.1)
// Autogenerated by Frugal Compiler (2.16.0)
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING

import 'dart:typed_data' show Uint8List;
Expand Down
2 changes: 1 addition & 1 deletion examples/dart/gen-dart/v1_music/lib/v1_music.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated by Frugal Compiler (2.15.1)
// Autogenerated by Frugal Compiler (2.16.0)
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING

library v1_music;
Expand Down
4 changes: 2 additions & 2 deletions examples/dart/gen-dart/v1_music/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: v1_music
version: 2.15.1
version: 2.16.0
description: Autogenerated by the frugal compiler
environment:
sdk: ^1.13.0
Expand All @@ -8,7 +8,7 @@ dependencies:
hosted:
name: frugal
url: https://pub.workiva.org
version: ^2.15.1
version: ^2.16.0
logging: ^0.11.2
thrift:
hosted:
Expand Down
2 changes: 1 addition & 1 deletion examples/dart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ dependencies:
hosted:
name: frugal
url: https://pub.workiva.org
version: ^2.15.1
version: ^2.16.0
logging: ^0.11.2
thrift:
hosted:
Expand Down
2 changes: 1 addition & 1 deletion examples/go/gen-go/v1/music/f_albumwinners_scope.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated by Frugal Compiler (2.15.1)
// Autogenerated by Frugal Compiler (2.16.0)
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING

package music
Expand Down
2 changes: 1 addition & 1 deletion examples/go/gen-go/v1/music/f_store_service.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated by Frugal Compiler (2.15.1)
// Autogenerated by Frugal Compiler (2.16.0)
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING

package music
Expand Down
2 changes: 1 addition & 1 deletion examples/go/gen-go/v1/music/f_types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated by Frugal Compiler (2.15.1)
// Autogenerated by Frugal Compiler (2.16.0)
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING

package music
Expand Down
4 changes: 2 additions & 2 deletions examples/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>com.workiva.frugal</groupId>
<artifactId>examples</artifactId>
<version>2.15.1</version>
<version>2.16.0</version>

<repositories>
<repository>
Expand All @@ -22,7 +22,7 @@
<dependency>
<groupId>com.workiva</groupId>
<artifactId>frugal</artifactId>
<version>2.15.1</version>
<version>2.16.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
Expand Down
Loading

0 comments on commit 0689625

Please sign in to comment.