[ 
https://issues.apache.org/jira/browse/BEAM-3612?focusedWorklogId=164595&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-164595
 ]

ASF GitHub Bot logged work on BEAM-3612:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 10/Nov/18 01:06
            Start Date: 10/Nov/18 01:06
    Worklog Time Spent: 10m 
      Work Description: lostluck commented on a change in pull request #7000: 
[BEAM-3612] Add a shim generator tool
URL: https://github.com/apache/beam/pull/7000#discussion_r232433710
 
 

 ##########
 File path: sdks/go/pkg/beam/util/starcgenx/starcgenx.go
 ##########
 @@ -0,0 +1,565 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package starcgenx is a Static Analysis Type Assertion shim and Registration 
Code Generator
+// which provides an extractor to extract types from a package, in order to 
generate
+// approprate shimsr a package so code can be generated for it.
+//
+// It's written for use by the starcgen tool, but separate to permit
+// alternative "go/importer" Importers for accessing types from imported 
packages.
+package starcgenx
+
+import (
+       "bytes"
+       "fmt"
+       "go/ast"
+       "go/token"
+       "go/types"
+       "strings"
+
+       "github.com/apache/beam/sdks/go/pkg/beam/util/shimx"
+)
+
+// NewExtractor returns an extractor for the given package.
+func NewExtractor(pkg string) *Extractor {
+       return &Extractor{
+               Package:     pkg,
+               functions:   make(map[string]struct{}),
+               types:       make(map[string]struct{}),
+               funcs:       make(map[string]*types.Signature),
+               emits:       make(map[string]shimx.Emitter),
+               iters:       make(map[string]shimx.Input),
+               imports:     make(map[string]struct{}),
+               allExported: true,
+       }
+}
+
+// Extractor contains and uniquifies the cache of types and things that need 
to be generated.
+type Extractor struct {
+       w       bytes.Buffer
+       Package string
+       debug   bool
+
+       // Ids is an optional slice of package local identifiers
+       Ids []string
+
+       // Register and uniquify the needed shims for each kind.
+       // Functions to Register
+       functions map[string]struct{}
+       // Types to Register (structs, essentially)
+       types map[string]struct{}
+       // FuncShims needed
+       funcs map[string]*types.Signature
+       // Emitter Shims needed
+       emits map[string]shimx.Emitter
+       // Iterator Shims needed
+       iters map[string]shimx.Input
+
+       // list of packages we need to import.
+       imports map[string]struct{}
+
+       allExported bool // Marks if all ptransforms are exported and available 
in main.
+}
+
+// Summary prints out a summary of the shims and registrations to
+// be generated to the buffer.
+func (e *Extractor) Summary() {
+       e.Print("\n")
+       e.Print("Summary\n")
+       e.Printf("All exported?: %v\n", e.allExported)
+       e.Printf("%d\t Functions\n", len(e.functions))
+       e.Printf("%d\t Types\n", len(e.types))
+       e.Printf("%d\t Shims\n", len(e.funcs))
+       e.Printf("%d\t Emits\n", len(e.emits))
+       e.Printf("%d\t Inputs\n", len(e.iters))
+}
+
+// lifecycleMethodName returns if the passed in string is one of the lifecycle 
method names used
+// by the Go SDK as DoFn or CombineFn lifecycle methods. These are the only 
methods that need
+// shims generated for them, as per beam/core/graph/fn.go
+// TODO(lostluck): Move this to beam/core/graph/fn.go, so it can stay up to 
date.
+func lifecycleMethodName(n string) bool {
+       switch n {
+       case "ProcessElement", "StartBundle", "FinishBundle", "Setup", 
"Teardown", "CreateAccumulator", "AddInput", "MergeAccumulators", 
"ExtractOutput", "Compact":
+               return true
+       default:
+               return false
+       }
+}
+
+// Bytes forwards to fmt.Fprint to the extractor buffer.
+func (e *Extractor) Bytes() []byte {
+       return e.w.Bytes()
+}
+
+// Print forwards to fmt.Fprint to the extractor buffer.
+func (e *Extractor) Print(s string) {
+       if e.debug {
+               fmt.Fprint(&e.w, s)
+       }
+}
+
+// Printf forwards to fmt.Printf to the extractor buffer.
+func (e *Extractor) Printf(f string, args ...interface{}) {
+       if e.debug {
+               fmt.Fprintf(&e.w, f, args...)
+       }
+}
+
+// FromAsts analyses the contents of a package
+func (e *Extractor) FromAsts(imp types.Importer, fset *token.FileSet, files 
[]*ast.File) error {
+       conf := types.Config{
+               Importer:                 imp,
+               IgnoreFuncBodies:         true,
+               DisableUnusedImportCheck: true,
+       }
+       info := &types.Info{
+               Defs: make(map[*ast.Ident]types.Object),
+       }
+       if len(e.Ids) != 0 {
+               // TODO(lostluck): This becomes unnnecessary iff we can figure 
out
+               // which ParDos are being passed to beam.ParDo or beam.Combine.
+               // If there are ids, we need to also look at function bodies, 
and uses.
+               var checkFuncBodies bool
+               for _, v := range e.Ids {
+                       if strings.Contains(v, ".") {
+                               checkFuncBodies = true
+                               break
+                       }
+               }
+               conf.IgnoreFuncBodies = !checkFuncBodies
+               info.Uses = make(map[*ast.Ident]types.Object)
+       }
+
+       if _, err := conf.Check(e.Package, fset, files, info); err != nil {
+               return fmt.Errorf("failed to type check package %s : %v", 
e.Package, err)
+       }
+
+       e.Print("/*\n")
+       var idsRequired, idsFound map[string]bool
+       if len(e.Ids) > 0 {
+               e.Printf("Filtering by %d identifiers: %q\n", len(e.Ids), 
strings.Join(e.Ids, ", "))
+               idsRequired = make(map[string]bool)
+               idsFound = make(map[string]bool)
+               for _, id := range e.Ids {
+                       idsRequired[id] = true
+               }
+       }
+       // TODO(rebo): Need to sort out struct types and their methods, so we 
only
 
 Review comment:
   Done. And this comment was addressed, so it could be removed.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 164595)
    Time Spent: 4h 40m  (was: 4.5h)

> Make it easy to generate type-specialized Go SDK reflectx.Funcs
> ---------------------------------------------------------------
>
>                 Key: BEAM-3612
>                 URL: https://issues.apache.org/jira/browse/BEAM-3612
>             Project: Beam
>          Issue Type: Improvement
>          Components: sdk-go
>            Reporter: Henning Rohde
>            Assignee: Robert Burke
>            Priority: Major
>          Time Spent: 4h 40m
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to