Skip to content

Commit

Permalink
Parse Provided and Default of "Input" beforehand
Browse files Browse the repository at this point in the history
  • Loading branch information
otiai10 committed Jul 18, 2018
1 parent 78cfed4 commit 9022dbf
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 15 deletions.
14 changes: 14 additions & 0 deletions entry.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package cwl

import (
"os"
"path/filepath"
)

// Entry represents fs entry, it means [File|Directory|Dirent]
type Entry struct {
Class string
Expand Down Expand Up @@ -67,3 +72,12 @@ func (_ Entry) New(i interface{}) Entry {
}
return dest
}

// LinkTo creates hardlink of this entry under destdir.
func (entry *Entry) LinkTo(destdir, srcdir string) error {
destpath := filepath.Join(destdir, filepath.Base(entry.Location))
if filepath.IsAbs(entry.Location) {
return os.Link(entry.Location, destpath)
}
return os.Link(filepath.Join(srcdir, entry.Location), destpath)
}
7 changes: 5 additions & 2 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Input struct {
Types []Type `json:"type"`
SecondaryFiles []SecondaryFile `json:"secondary_files"`
// Input.Provided is what provided by parameters.(json|yaml)
Provided Provided `json:"-"`
Provided *Provided `json:"-"`
// Requirement ..
RequiredType *Type
Requirements Requirements
Expand Down Expand Up @@ -49,6 +49,9 @@ func (input Input) New(i interface{}) *Input {
dest.SecondaryFiles = SecondaryFile{}.NewList(v)
}
}
if dest.Default != nil {
dest.Default.ID = dest.ID
}
case string:
dest.Types = Type{}.NewList(x)
case []interface{}:
Expand Down Expand Up @@ -213,7 +216,7 @@ func (input *Input) flattenWithRequiredType() []string {

// Flatten ...
func (input *Input) Flatten() []string {
if input.Provided.Raw == nil {
if input.Provided == nil {
// In case "input.Default == nil" should be validated by usage layer.
if input.Default != nil {
return input.Default.Flatten(input.Binding)
Expand Down
72 changes: 62 additions & 10 deletions input_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,86 @@ package cwl

import (
"fmt"
"io/ioutil"
"reflect"
)

// InputDefault represents "default" field in an element of "inputs".
type InputDefault struct {
Self interface{}
Kind reflect.Kind
ID string

Self interface{}
Kind reflect.Kind
Entry *Entry
Error error

// TODO: Refactor
Int int
}

// New constructs new "InputDefault".
func (_ InputDefault) New(i interface{}) *InputDefault {
func (d InputDefault) New(i interface{}) *InputDefault {
dest := &InputDefault{Self: i, Kind: reflect.TypeOf(i).Kind()}
switch v := i.(type) {
case nil:
return dest // do nothing
case int:
dest.Int = v
case map[string]interface{}: // It's "File" in most cases
dest.Entry, dest.Error = dest.EntryFromDictionary(v)
}
return dest
}

// Flatten ...
func (d *InputDefault) Flatten(binding *Binding) []string {
flattened := []string{}
switch v := d.Self.(type) {
case map[string]interface{}:
// TODO: more strict type casting ;(
class, ok := v["class"]
if ok && class == "File" {
flattened = append(flattened, fmt.Sprintf("%v", v["location"]))
}
if d.Entry != nil {
flattened = append(flattened, d.Entry.Location)
}
if binding != nil && binding.Prefix != "" {
flattened = append([]string{binding.Prefix}, flattened...)
}
return flattened
}

// EntryFromDictionary ...
func (d *InputDefault) EntryFromDictionary(dict map[string]interface{}) (*Entry, error) {
if dict == nil {
return nil, nil
}
class := dict["class"].(string)
location := dict["location"]
contents := dict["contents"]
if class == "" && location == nil && contents == nil {
return nil, nil
}
switch class {
case "File":
// Use location if specified
if location != nil {
return &Entry{
Class: class,
Location: fmt.Sprintf("%v", location),
File: File{},
}, nil
}
// Use contents if specified
if contentsstring, ok := contents.(string); ok {
tmpfile, err := ioutil.TempFile("/tmp", d.ID)
if err != nil {
return nil, err
}
defer tmpfile.Close()
if _, err := tmpfile.WriteString(contentsstring); err != nil {
return nil, err
}
return &Entry{
Class: class,
Location: tmpfile.Name(),
File: File{},
}, nil
}
}
return nil, nil
}
6 changes: 3 additions & 3 deletions input_provided.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ type Provided struct {
}

// New constructs new "Provided" struct.
func (provided Provided) New(id string, i interface{}) Provided {
dest := Provided{ID: id, Raw: i}
func (provided Provided) New(id string, i interface{}) *Provided {
dest := &Provided{ID: id, Raw: i}
switch v := i.(type) {
case nil:
return dest // do nothing
return nil
case int:
dest.Int = v
case map[interface{}]interface{}: // It's "File" in most cases
Expand Down

0 comments on commit 9022dbf

Please sign in to comment.