Skip to content

Commit

Permalink
Support for new yaml dsl apache#781
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Jul 3, 2019
1 parent 260f8dd commit b2ab99b
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 159 deletions.
5 changes: 0 additions & 5 deletions examples/routes.flow

This file was deleted.

11 changes: 11 additions & 0 deletions examples/routes.yaml
@@ -0,0 +1,11 @@
- id: "yaml"
from:
uri: "timer:tick"
parameters:
period: "5s"
steps:
- set-body:
constant: "Hello Yaml !!!"
- transform:
simple: "${body.toUpperCase()}"
- to: "log:info"
14 changes: 0 additions & 14 deletions pkg/apis/camel/v1alpha1/common_types.go
Expand Up @@ -35,20 +35,6 @@ type Artifact struct {
Target string `json:"target,omitempty" yaml:"target,omitempty"`
}

// Flow --
type Flow struct {
Steps []Step `json:"steps"`
}

// Flows are collections of Flow
type Flows []Flow

// Step --
type Step struct {
Kind string `json:"kind"`
URI string `json:"uri"`
}

// Failure --
type Failure struct {
Reason string `json:"reason"`
Expand Down
11 changes: 0 additions & 11 deletions pkg/apis/camel/v1alpha1/common_types_support.go
Expand Up @@ -19,8 +19,6 @@ package v1alpha1

import (
"fmt"

yaml2 "gopkg.in/yaml.v2"
)

func (in *Artifact) String() string {
Expand All @@ -30,12 +28,3 @@ func (in *Artifact) String() string {
func (spec ConfigurationSpec) String() string {
return fmt.Sprintf("%s=%s", spec.Type, spec.Value)
}

// Serialize serializes a Flow
func (flows Flows) Serialize() (string, error) {
res, err := yaml2.Marshal(flows)
if err != nil {
return "", err
}
return string(res), nil
}
6 changes: 3 additions & 3 deletions pkg/apis/camel/v1alpha1/integration_types.go
Expand Up @@ -120,8 +120,8 @@ const (
LanguageXML Language = "xml"
// LanguageKotlin --
LanguageKotlin Language = "kts"
// LanguageYamlFlow --
LanguageYamlFlow Language = "flow"
// LanguageYaml --
LanguageYaml Language = "yaml"
)

// Languages is the list of all supported languages
Expand All @@ -132,7 +132,7 @@ var Languages = []Language{
LanguageJavaScript,
LanguageXML,
LanguageKotlin,
LanguageYamlFlow,
LanguageYaml,
}

// IntegrationPhase --
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/camel/v1alpha1/integration_types_support_test.go
Expand Up @@ -31,7 +31,7 @@ func TestAllLanguages(t *testing.T) {
assert.Contains(t, Languages, LanguageGroovy)
assert.Contains(t, Languages, LanguageKotlin)
assert.Contains(t, Languages, LanguageXML)
assert.Contains(t, Languages, LanguageYamlFlow)
assert.Contains(t, Languages, LanguageYaml)
}

func TestLanguageFromName(t *testing.T) {
Expand Down
59 changes: 0 additions & 59 deletions pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 18 additions & 10 deletions pkg/metadata/metadata_uri_test.go
Expand Up @@ -258,21 +258,27 @@ func TestJavascript1(t *testing.T) {
assert.Len(t, metadata.ToURIs, 4)
}

const yamlFlow = `
- steps:
- kind: "endpoint"
const yaml = `
- from:
uri: "timer:tick"
- kind: "endpoint"
uri: "log:info"
steps:
- to: "log:info1"
- to:
uri: "log:info2"
- split:
tokenizer: 't'
steps:
- to: "log:info3"
`

func TestJYamlFlow(t *testing.T) {
func TestJYaml(t *testing.T) {
source := v1alpha1.SourceSpec{
DataSpec: v1alpha1.DataSpec{
Name: "test",
Content: yamlFlow,
Content: yaml,
},
Language: v1alpha1.LanguageYamlFlow,
Language: v1alpha1.LanguageYaml,
}

catalog, err := test.DefaultCatalog()
Expand All @@ -285,6 +291,8 @@ func TestJYamlFlow(t *testing.T) {
assert.Len(t, metadata.FromURIs, 1)

assert.NotEmpty(t, metadata.ToURIs)
assert.Contains(t, metadata.ToURIs, "log:info")
assert.Len(t, metadata.ToURIs, 1)
assert.Contains(t, metadata.ToURIs, "log:info1")
assert.Contains(t, metadata.ToURIs, "log:info2")
assert.Contains(t, metadata.ToURIs, "log:info3")
assert.Len(t, metadata.ToURIs, 3)
}
2 changes: 1 addition & 1 deletion pkg/trait/dependencies.go
Expand Up @@ -58,7 +58,7 @@ func (t *dependenciesTrait) Apply(e *Environment) error {
util.StringSliceUniqueAdd(&dependencies, "runtime:groovy")
case v1alpha1.LanguageKotlin:
util.StringSliceUniqueAdd(&dependencies, "runtime:kotlin")
case v1alpha1.LanguageYamlFlow:
case v1alpha1.LanguageYaml:
util.StringSliceUniqueAdd(&dependencies, "runtime:yaml")
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/util/source/inspector.go
Expand Up @@ -83,8 +83,8 @@ func InspectorForLanguage(catalog *camel.RuntimeCatalog, language v1alpha1.Langu
catalog: catalog,
},
}
case v1alpha1.LanguageYamlFlow:
return &YAMLFlowInspector{
case v1alpha1.LanguageYaml:
return &YAMLInspector{
baseInspector: baseInspector{
catalog: catalog,
},
Expand All @@ -105,6 +105,7 @@ func (i baseInspector) Extract(v1alpha1.SourceSpec, *Metadata) error {
func (i *baseInspector) discoverDependencies(source v1alpha1.SourceSpec, meta *Metadata) []string {
uris := util.StringSliceJoin(meta.FromURIs, meta.ToURIs)
candidates := strset.New()
candidates.Add(meta.Dependencies...)

for _, uri := range uris {
candidateComp := i.decodeComponent(uri)
Expand Down
101 changes: 101 additions & 0 deletions pkg/util/source/inspector_yaml.go
@@ -0,0 +1,101 @@
/*
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 source

import (
"fmt"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
yaml2 "gopkg.in/yaml.v2"
)

// YAMLInspector --
type YAMLInspector struct {
baseInspector
}

// Extract --
func (inspector YAMLInspector) Extract(source v1alpha1.SourceSpec, meta *Metadata) error {
definitions := make([]map[string]interface{}, 0)

if err := yaml2.Unmarshal([]byte(source.Content), &definitions); err != nil {
return err
}

for i := range definitions {
definition := definitions[i]

if len(definition) != 1 {
return fmt.Errorf("unable to parse step: %s", definition)
}

for k, v := range definition {
inspector.parseStep(k, v, meta)
}
}

meta.Dependencies = inspector.discoverDependencies(source, meta)

return nil
}

func (inspector YAMLInspector) parseStep(key string, content interface{}, meta *Metadata) error {
var maybeURI string

switch t := content.(type) {
case string:
maybeURI = t
case map[interface{}]interface{}:
if u, ok := t["uri"]; ok {
maybeURI = u.(string)
}
if u, ok := t["steps"]; ok {
steps := u.([]interface{})

for i := range steps {
step := steps[i].(map[interface{}]interface{})

if len(step) != 1 {
return fmt.Errorf("unable to parse step: %v", step)
}

for k, v := range step {
switch kt := k.(type) {
case fmt.Stringer:
inspector.parseStep(kt.String(), v, meta)
case string:
inspector.parseStep(kt, v, meta)
default:
return fmt.Errorf("unknown key type: %v, step: %v", k, step)
}
}
}
}
}

if maybeURI != "" {
switch key {
case "from":
meta.FromURIs = append(meta.FromURIs, maybeURI)
case "to":
meta.ToURIs = append(meta.ToURIs, maybeURI)
}
}

return nil
}
53 changes: 0 additions & 53 deletions pkg/util/source/inspector_yaml_flow.go

This file was deleted.

0 comments on commit b2ab99b

Please sign in to comment.