Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor source inspector #329

Merged
merged 1 commit into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 0 additions & 88 deletions pkg/metadata/dependencies.go

This file was deleted.

38 changes: 19 additions & 19 deletions pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ import (
func ExtractAll(sources []v1alpha1.SourceSpec) IntegrationMetadata {
// neutral metadata
meta := IntegrationMetadata{
Dependencies: []string{},
FromURIs: []string{},
ToURIs: []string{},
Metadata: src.Metadata{
FromURIs: []string{},
ToURIs: []string{},
Dependencies: []string{},
},
PassiveEndpoints: true,
RequiresHTTPService: false,
}
Expand All @@ -54,9 +56,11 @@ func merge(m1 IntegrationMetadata, m2 IntegrationMetadata) IntegrationMetadata {
}
sort.Strings(allDependencies)
return IntegrationMetadata{
FromURIs: append(m1.FromURIs, m2.FromURIs...),
ToURIs: append(m1.ToURIs, m2.ToURIs...),
Dependencies: allDependencies,
Metadata: src.Metadata{
FromURIs: append(m1.FromURIs, m2.FromURIs...),
ToURIs: append(m1.ToURIs, m2.ToURIs...),
Dependencies: allDependencies,
},
RequiresHTTPService: m1.RequiresHTTPService || m2.RequiresHTTPService,
PassiveEndpoints: m1.PassiveEndpoints && m2.PassiveEndpoints,
}
Expand All @@ -65,20 +69,16 @@ func merge(m1 IntegrationMetadata, m2 IntegrationMetadata) IntegrationMetadata {
// Extract returns metadata information from the source code
func Extract(source v1alpha1.SourceSpec) IntegrationMetadata {
language := source.InferLanguage()

m := IntegrationMetadata{}

// TODO: handle error
fromURIs, _ := src.InspectorForLanguage(language).FromURIs(source)
// TODO:: handle error
toURIs, _ := src.InspectorForLanguage(language).ToURIs(source)
dependencies := discoverDependencies(source, fromURIs, toURIs)
requiresHTTPService := requiresHTTPService(source, fromURIs)
passiveEndpoints := hasOnlyPassiveEndpoints(source, fromURIs)
return IntegrationMetadata{
FromURIs: fromURIs,
ToURIs: toURIs,
Dependencies: dependencies,
RequiresHTTPService: requiresHTTPService,
PassiveEndpoints: passiveEndpoints,
}
_ = src.InspectorForLanguage(language).Extract(source, &m.Metadata)

m.RequiresHTTPService = requiresHTTPService(source, m.FromURIs)
m.PassiveEndpoints = hasOnlyPassiveEndpoints(source, m.FromURIs)

return m
}

// Each --
Expand Down
9 changes: 3 additions & 6 deletions pkg/metadata/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ limitations under the License.

package metadata

import "github.com/apache/camel-k/pkg/util/source"

// IntegrationMetadata contains aggregate metadata about all Camel routes in a integrations
type IntegrationMetadata struct {
// All starting URIs of defined routes
FromURIs []string
// All end URIs of defined routes
ToURIs []string
// All inferred dependencies required to run the integration
Dependencies []string
source.Metadata
// RequiresHTTPService indicates if the integration needs to be invoked through HTTP
RequiresHTTPService bool
// PassiveEndpoints indicates that the integration contains only passive endpoints that are activated from
Expand Down
81 changes: 56 additions & 25 deletions pkg/util/source/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ package source

import (
"regexp"
"sort"
"strings"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util"
"github.com/apache/camel-k/pkg/util/camel"
"github.com/scylladb/go-set/strset"
)

var (
Expand All @@ -32,12 +37,15 @@ var (
doubleQuotedTo = regexp.MustCompile(`\.to\s*\(\s*"([a-z0-9-]+:[^"]+)"\s*\)`)
doubleQuotedToD = regexp.MustCompile(`\.toD\s*\(\s*"([a-z0-9-]+:[^"]+)"\s*\)`)
doubleQuotedToF = regexp.MustCompile(`\.toF\s*\(\s*"([a-z0-9-]+:[^"]+)"[^)]*\)`)

additionalDependencies = map[string]string{
".*JsonLibrary\\.Jackson.*": "camel:jackson",
}
)

// Inspector --
type Inspector interface {
FromURIs(v1alpha1.SourceSpec) ([]string, error)
ToURIs(v1alpha1.SourceSpec) ([]string, error)
Extract(v1alpha1.SourceSpec, *Metadata) error
}

// InspectorForLanguage --
Expand All @@ -56,34 +64,57 @@ func InspectorForLanguage(language v1alpha1.Language) Inspector {
case v1alpha1.LanguageYamlFlow:
return &YAMLFlowInspector{}
}
return &noInspector{}
return &baseInspector{}
}

func findAllDistinctStringSubmatch(data string, regexps ...*regexp.Regexp) []string {
candidates := make([]string, 0)
alreadyFound := make(map[string]bool)
for _, reg := range regexps {
hits := reg.FindAllStringSubmatch(data, -1)
for _, hit := range hits {
if len(hit) > 1 {
for _, match := range hit[1:] {
if _, ok := alreadyFound[match]; !ok {
alreadyFound[match] = true
candidates = append(candidates, match)
}
}
}
}
}
return candidates
type baseInspector struct {
}

type noInspector struct {
func (i baseInspector) Extract(v1alpha1.SourceSpec, *Metadata) error {
return nil
}

func (i noInspector) FromURIs(source v1alpha1.SourceSpec) ([]string, error) {
return []string{}, nil
// discoverDependencies returns a list of dependencies required by the given source code
func (i *baseInspector) discoverDependencies(source v1alpha1.SourceSpec, meta *Metadata) []string {
uris := util.StringSliceJoin(meta.FromURIs, meta.ToURIs)
candidates := strset.New()

for _, uri := range uris {
candidateComp := i.decodeComponent(uri)
if candidateComp != "" {
candidates.Add(candidateComp)
}
}

for pattern, dep := range additionalDependencies {
pat := regexp.MustCompile(pattern)
if pat.MatchString(source.Content) {
candidates.Add(dep)
}
}

components := candidates.List()

sort.Strings(components)

return components
}
func (i noInspector) ToURIs(source v1alpha1.SourceSpec) ([]string, error) {
return []string{}, nil

func (i *baseInspector) decodeComponent(uri string) string {
uriSplit := strings.SplitN(uri, ":", 2)
if len(uriSplit) < 2 {
return ""
}
uriStart := uriSplit[0]
if component := camel.Runtime.GetArtifactByScheme(uriStart); component != nil {
artifactID := component.ArtifactID
if component.GroupID == "org.apache.camel" && strings.HasPrefix(artifactID, "camel-") {
return "camel:" + artifactID[6:]
}
if component.GroupID == "org.apache.camel.k" && strings.HasPrefix(artifactID, "camel-") {
return "camel-k:" + artifactID[6:]
}
return "mvn:" + component.GroupID + ":" + artifactID + ":" + component.Version
}
return ""
}
26 changes: 14 additions & 12 deletions pkg/util/source/inspector_groovy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,24 @@ limitations under the License.

package source

import "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
import (
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util"
)

// GroovyInspector --
type GroovyInspector struct {
baseInspector
}

// FromURIs --
func (i GroovyInspector) FromURIs(source v1alpha1.SourceSpec) ([]string, error) {
answer := findAllDistinctStringSubmatch(
// Extract --
func (i GroovyInspector) Extract(source v1alpha1.SourceSpec, meta *Metadata) error {
from := util.FindAllDistinctStringSubmatch(
source.Content,
singleQuotedFrom,
doubleQuotedFrom,
)

return answer, nil
}

// ToURIs --
func (i GroovyInspector) ToURIs(source v1alpha1.SourceSpec) ([]string, error) {
answer := findAllDistinctStringSubmatch(
to := util.FindAllDistinctStringSubmatch(
source.Content,
singleQuotedTo,
doubleQuotedTo,
Expand All @@ -46,5 +44,9 @@ func (i GroovyInspector) ToURIs(source v1alpha1.SourceSpec) ([]string, error) {
doubleQuotedToF,
)

return answer, nil
meta.FromURIs = append(meta.FromURIs, from...)
meta.ToURIs = append(meta.ToURIs, to...)
meta.Dependencies = i.discoverDependencies(source, meta)

return nil
}
26 changes: 14 additions & 12 deletions pkg/util/source/inspector_java_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,24 @@ limitations under the License.

package source

import "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
import (
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util"
)

// JavaScriptInspector --
type JavaScriptInspector struct {
baseInspector
}

// FromURIs --
func (i JavaScriptInspector) FromURIs(source v1alpha1.SourceSpec) ([]string, error) {
answer := findAllDistinctStringSubmatch(
// Extract --
func (i JavaScriptInspector) Extract(source v1alpha1.SourceSpec, meta *Metadata) error {
from := util.FindAllDistinctStringSubmatch(
source.Content,
singleQuotedFrom,
doubleQuotedFrom,
)

return answer, nil
}

// ToURIs --
func (i JavaScriptInspector) ToURIs(source v1alpha1.SourceSpec) ([]string, error) {
answer := findAllDistinctStringSubmatch(
to := util.FindAllDistinctStringSubmatch(
source.Content,
singleQuotedTo,
doubleQuotedTo,
Expand All @@ -46,5 +44,9 @@ func (i JavaScriptInspector) ToURIs(source v1alpha1.SourceSpec) ([]string, error
doubleQuotedToF,
)

return answer, nil
meta.FromURIs = append(meta.FromURIs, from...)
meta.ToURIs = append(meta.ToURIs, to...)
meta.Dependencies = i.discoverDependencies(source, meta)

return nil
}
Loading