Skip to content

Commit

Permalink
fix singular compact column names
Browse files Browse the repository at this point in the history
If a COMPACT STORAGE CF has only one component defined, then the column
name should not be a composite.
  • Loading branch information
wadey committed Oct 18, 2013
1 parent 9a8cdcc commit 4518cec
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions src/gossie/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (m *sparseMapping) MarshalComponent(component interface{}, position int) ([
return b, nil
}

func (m *sparseMapping) startMap(source interface{}) (*Row, *reflect.Value, *structInspection, []byte, error) {
func (m *sparseMapping) startMap(source interface{}, compact bool) (*Row, *reflect.Value, *structInspection, []byte, error) {
v, si, err := validateAndInspectStruct(source)
if err != nil {
return nil, nil, nil, nil, err
Expand All @@ -179,24 +179,37 @@ func (m *sparseMapping) startMap(source interface{}) (*Row, *reflect.Value, *str
}

// prepare composite, if needed
composite := make([]byte, 0)
for _, c := range m.components {
var composite []byte
if compact && len(m.components) == 1 {
c := m.components[0]
if f, found := si.goFields[c]; found {
b, err := f.marshalValue(v)
composite, err = f.marshalValue(v)
if err != nil {
return nil, nil, nil, nil, err
}
composite = append(composite, packComposite(b, eocEquals)...)
} else {
return nil, nil, nil, nil, errors.New(fmt.Sprint("Mapping component field ", c, " not found in passed struct of type ", v.Type().Name()))
}
} else {
composite = make([]byte, 0)
for _, c := range m.components {
if f, found := si.goFields[c]; found {
b, err := f.marshalValue(v)
if err != nil {
return nil, nil, nil, nil, err
}
composite = append(composite, packComposite(b, eocEquals)...)
} else {
return nil, nil, nil, nil, errors.New(fmt.Sprint("Mapping component field ", c, " not found in passed struct of type ", v.Type().Name()))
}
}
}

return row, v, si, composite, nil
}

func (m *sparseMapping) Map(source interface{}) (*Row, error) {
row, v, si, composite, err := m.startMap(source)
row, v, si, composite, err := m.startMap(source, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -375,7 +388,7 @@ func (m *compactMapping) Cf() string {
}

func (m *compactMapping) Map(source interface{}) (*Row, error) {
row, v, si, composite, err := m.startMap(source)
row, v, si, composite, err := m.startMap(source, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -408,12 +421,24 @@ func (m *compactMapping) Unmap(destination interface{}, provider RowProvider) er
return err
}

components, err := m.extractComponents(column, v, 0)
if err != nil {
return err
}
if err := m.unmapComponents(v, si, components); err != nil {
return err
if len(m.components) == 1 {
c := m.components[0]
if f, found := si.goFields[c]; found {
err := f.unmarshalValue(column.Name, v)
if err != nil {
return err
}
} else {
return errors.New(fmt.Sprint("Mapping component field ", c, " not found in passed struct of type ", v.Type().Name()))
}
} else {
components, err := m.extractComponents(column, v, 0)
if err != nil {
return err
}
if err := m.unmapComponents(v, si, components); err != nil {
return err
}
}
if f, found := si.goFields[m.value]; found {
err := f.unmarshalValue(column.Value, v)
Expand Down

0 comments on commit 4518cec

Please sign in to comment.