Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
chilland committed Feb 24, 2017
1 parent 3dd5fc1 commit 5013033
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions kazaam.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,23 @@ type spec struct {
Require bool `json:"require,omitempty"`
}

type KazaamError struct {
// Error provids an error mess (ErrMsg) and integer code (ErrType) for
// errors thrown by a transform
type Error struct {
ErrMsg string
ErrType int
}

const (
ParseError int = iota // There was an error parsing the data
RequireError int = iota // The JSON path does not exist and is required
SpecError int = iota // The kazaam specification is not properly formatted
// ParseError is thrown when there is a JSON parsing error
ParseError int = iota
// RequireError is thrown when the JSON path does not exist and is required
RequireError int = iota
// SpecError is thrown when the kazaam specification is not properly formatted
SpecError int = iota
)

func (e *KazaamError) Error() string {
func (e *Error) Error() string {
switch e.ErrType {
case ParseError:
return fmt.Sprintf("ParseError - %s", e.ErrMsg)
Expand All @@ -66,14 +71,14 @@ func (s *spec) UnmarshalJSON(b []byte) (err error) {
if err = json.Unmarshal(b, &j); err == nil {
*s = spec(j)
if s.Operation == nil {
err = &KazaamError{ErrMsg: "Spec must contain an \"operation\" field", ErrType: SpecError}
err = &Error{ErrMsg: "Spec must contain an \"operation\" field", ErrType: SpecError}
return
}
if _, ok := validSpecTypes[*s.Operation]; ok == false {
err = &KazaamError{ErrMsg: "Invalid spec operation specified", ErrType: SpecError}
err = &Error{ErrMsg: "Invalid spec operation specified", ErrType: SpecError}
}
if s.Spec != nil && len(*s.Spec) < 1 {
err = &KazaamError{ErrMsg: "Spec must contain at least one element", ErrType: SpecError}
err = &Error{ErrMsg: "Spec must contain at least one element", ErrType: SpecError}
return
}
return
Expand Down Expand Up @@ -205,7 +210,7 @@ func transformDefault(spec *spec, data *simplejson.Json) (*simplejson.Json, erro
func transformExtract(spec *spec, data *simplejson.Json) (*simplejson.Json, error) {
outPath, ok := (*spec.Spec)["path"]
if !ok {
return nil, &KazaamError{ErrMsg: fmt.Sprintf("Unable to get path"), ErrType: SpecError}
return nil, &Error{ErrMsg: fmt.Sprintf("Unable to get path"), ErrType: SpecError}
}
outData, err := getJSONPath(data, outPath.(string), spec.Require)
return outData, err
Expand All @@ -228,12 +233,12 @@ func transformShift(spec *spec, data *simplejson.Json) (*simplejson.Json, error)
for _, vItem := range v.([]interface{}) {
vItemStr, found := vItem.(string)
if !found {
return nil, &KazaamError{ErrMsg: fmt.Sprintf("Warn: Unable to coerce element to json string: %v", vItem), ErrType: ParseError}
return nil, &Error{ErrMsg: fmt.Sprintf("Warn: Unable to coerce element to json string: %v", vItem), ErrType: ParseError}
}
keyList = append(keyList, vItemStr)
}
default:
return nil, &KazaamError{ErrMsg: fmt.Sprintf("Warn: Unknown type in message for key: %s", k), ErrType: ParseError}
return nil, &Error{ErrMsg: fmt.Sprintf("Warn: Unknown type in message for key: %s", k), ErrType: ParseError}
}

// iterate over keys to evaluate
Expand Down Expand Up @@ -267,11 +272,11 @@ func transformShift(spec *spec, data *simplejson.Json) (*simplejson.Json, error)
func transformConcat(spec *spec, data *simplejson.Json) (*simplejson.Json, error) {
sourceList, sourceOk := (*spec.Spec)["sources"]
if !sourceOk {
return nil, &KazaamError{ErrMsg: fmt.Sprintf("Unable to get sources"), ErrType: SpecError}
return nil, &Error{ErrMsg: fmt.Sprintf("Unable to get sources"), ErrType: SpecError}
}
targetPath, targetOk := (*spec.Spec)["targetPath"]
if !targetOk {
return nil, &KazaamError{ErrMsg: fmt.Sprintf("Unable to get targetPath"), ErrType: SpecError}
return nil, &Error{ErrMsg: fmt.Sprintf("Unable to get targetPath"), ErrType: SpecError}
}
delimiter, delimOk := (*spec.Spec)["delim"]
if !delimOk {
Expand All @@ -292,7 +297,7 @@ func transformConcat(spec *spec, data *simplejson.Json) (*simplejson.Json, error
valueNodePtr, err := getJSONPath(data, path.(string), spec.Require)
switch {
case err != nil && spec.Require == true:
return nil, &KazaamError{ErrMsg: fmt.Sprintf("Path does not exist"), ErrType: RequireError}
return nil, &Error{ErrMsg: fmt.Sprintf("Path does not exist"), ErrType: RequireError}
case err != nil:
value = ""
default:
Expand All @@ -311,7 +316,7 @@ func transformConcat(spec *spec, data *simplejson.Json) (*simplejson.Json, error
}
}
} else {
return nil, &KazaamError{ErrMsg: fmt.Sprintf("Error processing %v: must have either value or path specified", vItem), ErrType: SpecError}
return nil, &Error{ErrMsg: fmt.Sprintf("Error processing %v: must have either value or path specified", vItem), ErrType: SpecError}
}
}
outString += value.(string)
Expand All @@ -326,7 +331,7 @@ func transformConcat(spec *spec, data *simplejson.Json) (*simplejson.Json, error

func transformCoalesce(spec *spec, data *simplejson.Json) (*simplejson.Json, error) {
if spec.Require == true {
return nil, &KazaamError{ErrMsg: fmt.Sprintf("Invalid spec. Coalesce does not support \"require\""), ErrType: SpecError}
return nil, &Error{ErrMsg: fmt.Sprintf("Invalid spec. Coalesce does not support \"require\""), ErrType: SpecError}
}
for k, v := range *spec.Spec {
outPath := strings.Split(k, ".")
Expand All @@ -339,12 +344,12 @@ func transformCoalesce(spec *spec, data *simplejson.Json) (*simplejson.Json, err
for _, vItem := range v.([]interface{}) {
vItemStr, found := vItem.(string)
if !found {
return nil, &KazaamError{ErrMsg: fmt.Sprintf("Warn: Unable to coerce element to json string: %v", vItem), ErrType: ParseError}
return nil, &Error{ErrMsg: fmt.Sprintf("Warn: Unable to coerce element to json string: %v", vItem), ErrType: ParseError}
}
keyList = append(keyList, vItemStr)
}
default:
return nil, &KazaamError{ErrMsg: fmt.Sprintf("Warn: Expected list in message for key: %s", k), ErrType: ParseError}
return nil, &Error{ErrMsg: fmt.Sprintf("Warn: Expected list in message for key: %s", k), ErrType: ParseError}
}

// iterate over keys to evaluate
Expand Down Expand Up @@ -384,7 +389,7 @@ func getJSONPath(j *simplejson.Json, path string, pathRequired bool) (*simplejso
if pathRequired == true {
_, exists := jin.CheckGet(objKey)
if exists != true {
return nil, &KazaamError{ErrMsg: fmt.Sprintf("Path does not exist"), ErrType: RequireError}
return nil, &Error{ErrMsg: fmt.Sprintf("Path does not exist"), ErrType: RequireError}
}
}
jin = jin.Get(objKey)
Expand Down Expand Up @@ -419,7 +424,7 @@ func getJSONPath(j *simplejson.Json, path string, pathRequired bool) (*simplejso
if pathRequired == true {
_, exists := jin.CheckGet(k)
if exists != true {
return nil, &KazaamError{ErrMsg: fmt.Sprintf("Path does not exist"), ErrType: RequireError}
return nil, &Error{ErrMsg: fmt.Sprintf("Path does not exist"), ErrType: RequireError}
}
}
jin = jin.Get(k)
Expand Down

0 comments on commit 5013033

Please sign in to comment.