forked from apache/camel-k
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix apache#1470: support inline YAML
- Loading branch information
1 parent
31f05df
commit 86b3da3
Showing
11 changed files
with
207 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
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 flows | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"io/ioutil" | ||
|
||
v1 "github.com/apache/camel-k/pkg/apis/camel/v1" | ||
yaml2 "gopkg.in/yaml.v2" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
) | ||
|
||
// UnmarshalString reads flows contained in a string | ||
func UnmarshalString(flowsString string) ([]v1.Flow, error) { | ||
return Unmarshal(bytes.NewReader([]byte(flowsString))) | ||
} | ||
|
||
// Unmarshal flows from a stream | ||
func Unmarshal(reader io.Reader) ([]v1.Flow, error) { | ||
buffered, err := ioutil.ReadAll(reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var flows []v1.Flow | ||
// Using the Kubernetes decoder to turn them into JSON before unmarshal. | ||
// This avoids having map[interface{}]interface{} objects which are not JSON compatible. | ||
jsonData, err := yaml.ToJSON(buffered) | ||
if err = json.Unmarshal(jsonData, &flows); err != nil { | ||
return nil, err | ||
} | ||
return flows, err | ||
} | ||
|
||
// Marshal flows as byte array | ||
func Marshal(flows []v1.Flow) ([]byte, error) { | ||
return yaml2.Marshal(flows) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
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 flows | ||
|
||
import ( | ||
"bytes" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestReadWriteYaml(t *testing.T) { | ||
// yaml in conventional form as marshalled by the go runtime | ||
yaml := `- from: | ||
steps: | ||
- to: log:info | ||
uri: timer:tick | ||
` | ||
yamlReader := bytes.NewReader([]byte(yaml)) | ||
flows, err := Unmarshal(yamlReader) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, flows) | ||
assert.Len(t, flows, 1) | ||
assert.NotNil(t, flows[0]["from"]) | ||
assert.Nil(t, flows[0]["xx"]) | ||
|
||
clone, err := Marshal(flows) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, clone) | ||
assert.Equal(t, yaml, string(clone)) | ||
} |