Skip to content
This repository has been archived by the owner on May 16, 2022. It is now read-only.

Commit

Permalink
Add some type safety for film advance mode
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Shishkin <[email protected]>
  • Loading branch information
teran committed Dec 21, 2019
1 parent 43a154b commit 8c44434
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 21 deletions.
8 changes: 6 additions & 2 deletions parser/csv_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,12 @@ func parseShootingMode(s string) (*types.ShootingMode, error) {
return types.ShootingModeFromString(*sms)
}

func parseFilmAdvanceMode(s string) (*string, error) {
return parseString(s)
func parseFilmAdvanceMode(s string) (*types.FilmAdvanceMode, error) {
fams, err := parseString(s)
if err != nil {
return nil, err
}
return types.FilmAdvanceModeFromString(*fams)
}

func parseAFMode(s string) (*types.AFMode, error) {
Expand Down
37 changes: 19 additions & 18 deletions parser/csv_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestTwoFilmsInSingleCSV(t *testing.T) {
ShootingMode: ptrShootingMode(types.ShootingModeAperturePriorityAE),
ExposureCompensation: ptrFloat64(0),
FlashCompensation: ptrFloat64(0),
FilmAdvanceMode: ptrString("Single-frame"),
FilmAdvanceMode: ptrFilmAdvanceMode(types.FilmAdvanceModeSingleFrame),
AFMode: ptrAFMode(types.AFModeOneShotAF),
Timestamp: mustParseTimestamp(t, "10/7/2019T20:02:18", tz, types.TimestampFormatUS),
MultipleExposure: ptrString("OFF"),
Expand All @@ -69,7 +69,7 @@ func TestTwoFilmsInSingleCSV(t *testing.T) {
FlashMode: ptrFlashMode(types.FlashModeOff),
MeteringMode: ptrMeteringMode(types.MeteringModeEvaluative),
ShootingMode: ptrShootingMode(types.ShootingModeAperturePriorityAE),
FilmAdvanceMode: ptrString("Single-frame"),
FilmAdvanceMode: ptrFilmAdvanceMode(types.FilmAdvanceModeSingleFrame),
AFMode: ptrAFMode(types.AFModeOneShotAF),
Timestamp: mustParseTimestamp(t, "10/7/2019T20:02:29", tz, types.TimestampFormatUS),
MultipleExposure: ptrString("OFF"),
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestTwoFilmsInSingleCSV(t *testing.T) {
FlashMode: ptrFlashMode(types.FlashModeOff),
MeteringMode: ptrMeteringMode(types.MeteringModeEvaluative),
ShootingMode: ptrShootingMode(types.ShootingModeProgramAE),
FilmAdvanceMode: ptrString("Single-frame"),
FilmAdvanceMode: ptrFilmAdvanceMode(types.FilmAdvanceModeSingleFrame),
AFMode: ptrAFMode(types.AFModeOneShotAF),
Timestamp: mustParseTimestamp(t, "10/13/2019T14:55:38", tz, types.TimestampFormatUS),
MultipleExposure: ptrString("OFF"),
Expand All @@ -120,7 +120,7 @@ func TestTwoFilmsInSingleCSV(t *testing.T) {
FlashMode: ptrFlashMode(types.FlashModeOff),
MeteringMode: ptrMeteringMode(types.MeteringModeEvaluative),
ShootingMode: ptrShootingMode(types.ShootingModeAperturePriorityAE),
FilmAdvanceMode: ptrString("Single-frame"),
FilmAdvanceMode: ptrFilmAdvanceMode(types.FilmAdvanceModeSingleFrame),
AFMode: ptrAFMode(types.AFModeOneShotAF),
Timestamp: mustParseTimestamp(t, "10/13/2019T14:55:55", tz, types.TimestampFormatUS),
MultipleExposure: ptrString("OFF"),
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestFilmWithPartialTimestampsEUFormatted(t *testing.T) {
ExposureCompensation: ptrFloat64(0),
FlashCompensation: ptrFloat64(0),
ShootingMode: ptrShootingMode(types.ShootingModeAperturePriorityAE),
FilmAdvanceMode: ptrString("Single-frame"),
FilmAdvanceMode: ptrFilmAdvanceMode(types.FilmAdvanceModeSingleFrame),
AFMode: ptrAFMode(types.AFModeOneShotAF),
Timestamp: mustParseTimestamp(t, "15/07/2019T20:02:18", tz, types.TimestampFormatEU),
MultipleExposure: ptrString("OFF"),
Expand All @@ -192,7 +192,7 @@ func TestFilmWithPartialTimestampsEUFormatted(t *testing.T) {
FlashMode: ptrFlashMode(types.FlashModeOff),
MeteringMode: ptrMeteringMode(types.MeteringModeEvaluative),
ShootingMode: ptrShootingMode(types.ShootingModeAperturePriorityAE),
FilmAdvanceMode: ptrString("Single-frame"),
FilmAdvanceMode: ptrFilmAdvanceMode(types.FilmAdvanceModeSingleFrame),
AFMode: ptrAFMode(types.AFModeOneShotAF),
Timestamp: nil,
MultipleExposure: ptrString("OFF"),
Expand Down Expand Up @@ -422,7 +422,7 @@ func TestPartialData(t *testing.T) {
FlashMode: ptrFlashMode(types.FlashModeOff),
MeteringMode: ptrMeteringMode(types.MeteringModeEvaluative),
ShootingMode: ptrShootingMode(types.ShootingModeAperturePriorityAE),
FilmAdvanceMode: ptrString("Single-frame"),
FilmAdvanceMode: ptrFilmAdvanceMode(types.FilmAdvanceModeSingleFrame),
},
{
Flag: ptrBool(false),
Expand All @@ -437,7 +437,7 @@ func TestPartialData(t *testing.T) {
FlashMode: ptrFlashMode(types.FlashModeOff),
MeteringMode: ptrMeteringMode(types.MeteringModeEvaluative),
ShootingMode: ptrShootingMode(types.ShootingModeAperturePriorityAE),
FilmAdvanceMode: ptrString("Single-frame"),
FilmAdvanceMode: ptrFilmAdvanceMode(types.FilmAdvanceModeSingleFrame),
AFMode: ptrAFMode(types.AFModeOneShotAF),
},
{
Expand All @@ -453,7 +453,7 @@ func TestPartialData(t *testing.T) {
FlashMode: ptrFlashMode(types.FlashModeOff),
MeteringMode: ptrMeteringMode(types.MeteringModeEvaluative),
ShootingMode: ptrShootingMode(types.ShootingModeAperturePriorityAE),
FilmAdvanceMode: ptrString("Single-frame"),
FilmAdvanceMode: ptrFilmAdvanceMode(types.FilmAdvanceModeSingleFrame),
AFMode: ptrAFMode(types.AFModeOneShotAF),
Timestamp: mustParseTimestamp(t, "07/10/2019T20:02:18", tz, types.TimestampFormatEU),
},
Expand All @@ -470,7 +470,7 @@ func TestPartialData(t *testing.T) {
FlashMode: ptrFlashMode(types.FlashModeOff),
MeteringMode: ptrMeteringMode(types.MeteringModeEvaluative),
ShootingMode: ptrShootingMode(types.ShootingModeAperturePriorityAE),
FilmAdvanceMode: ptrString("Single-frame"),
FilmAdvanceMode: ptrFilmAdvanceMode(types.FilmAdvanceModeSingleFrame),
AFMode: ptrAFMode(types.AFModeOneShotAF),
MultipleExposure: ptrString("OFF"),
Timestamp: mustParseTimestamp(t, "07/10/2019T20:02:18", tz, types.TimestampFormatEU),
Expand Down Expand Up @@ -508,11 +508,12 @@ func mustParseTimestamp(t *testing.T, ts string, tz *time.Location, tf string) *
return tt
}

func ptrInt64(i int64) *int64 { return &i }
func ptrFloat64(f float64) *float64 { return &f }
func ptrString(s string) *string { return &s }
func ptrBool(b bool) *bool { return &b }
func ptrMeteringMode(mm types.MeteringMode) *types.MeteringMode { return &mm }
func ptrShootingMode(sm types.ShootingMode) *types.ShootingMode { return &sm }
func ptrAFMode(am types.AFMode) *types.AFMode { return &am }
func ptrFlashMode(fm types.FlashMode) *types.FlashMode { return &fm }
func ptrInt64(i int64) *int64 { return &i }
func ptrFloat64(f float64) *float64 { return &f }
func ptrString(s string) *string { return &s }
func ptrBool(b bool) *bool { return &b }
func ptrMeteringMode(mm types.MeteringMode) *types.MeteringMode { return &mm }
func ptrShootingMode(sm types.ShootingMode) *types.ShootingMode { return &sm }
func ptrAFMode(am types.AFMode) *types.AFMode { return &am }
func ptrFlashMode(fm types.FlashMode) *types.FlashMode { return &fm }
func ptrFilmAdvanceMode(fam types.FilmAdvanceMode) *types.FilmAdvanceMode { return &fam }
64 changes: 64 additions & 0 deletions types/film_advance_mode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package types

import (
"strings"

"github.com/pkg/errors"
)

// FilmAdvanceMode ...
type FilmAdvanceMode string

const (
// FilmAdvanceModeSingleFrame ...
FilmAdvanceModeSingleFrame FilmAdvanceMode = "Single-frame"

// FilmAdvanceModeContinuousBodyOnly ...
FilmAdvanceModeContinuousBodyOnly FilmAdvanceMode = "Continuous (body only)"

// FilmAdvanceModeLowSpeedContinuous ...
FilmAdvanceModeLowSpeedContinuous FilmAdvanceMode = "Low-speed continuous"

// FilmAdvanceModeHighSpeedContinuous ...
FilmAdvanceModeHighSpeedContinuous FilmAdvanceMode = "High-speed continuous"

// FilmAdvanceModeUltraHighSpeedContinuous ...
FilmAdvanceModeUltraHighSpeedContinuous FilmAdvanceMode = "Ultra-high-speed continuous"

// FilmAdvanceMode2secSelfTimer ...
FilmAdvanceMode2secSelfTimer FilmAdvanceMode = "2-sec. self-timer"

// FilmAdvanceMode10secSelfTimer ...
FilmAdvanceMode10secSelfTimer FilmAdvanceMode = "10-sec. self-timer"
)

// FilmAdvanceModeFromString ...
func FilmAdvanceModeFromString(s string) (fam *FilmAdvanceMode, err error) {
s = strings.TrimSpace(s)

var famv FilmAdvanceMode
switch FilmAdvanceMode(s) {
case FilmAdvanceMode10secSelfTimer:
famv = FilmAdvanceMode10secSelfTimer
case FilmAdvanceMode2secSelfTimer:
famv = FilmAdvanceMode2secSelfTimer
case FilmAdvanceModeContinuousBodyOnly:
famv = FilmAdvanceModeContinuousBodyOnly
case FilmAdvanceModeHighSpeedContinuous:
famv = FilmAdvanceModeHighSpeedContinuous
case FilmAdvanceModeLowSpeedContinuous:
famv = FilmAdvanceModeLowSpeedContinuous
case FilmAdvanceModeSingleFrame:
famv = FilmAdvanceModeSingleFrame
case FilmAdvanceModeUltraHighSpeedContinuous:
famv = FilmAdvanceModeUltraHighSpeedContinuous
default:
err = errors.Errorf("error parsing FilmAdvanceMode: unknown value `%s`", s)
}

return &famv, err
}

func (famv *FilmAdvanceMode) String() string {
return string(*famv)
}
90 changes: 90 additions & 0 deletions types/film_advance_mode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package types

import (
"errors"
"testing"

"github.com/stretchr/testify/require"
)

func TestFilmAdvanceMode(t *testing.T) {
r := require.New(t)

type testCase struct {
name string
input string
expOutput *FilmAdvanceMode
expError error
}

tcs := []testCase{
{
name: "Single-frame",
input: "Single-frame",
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeSingleFrame),
},
{
name: "Continuous (body only)",
input: "Continuous (body only)",
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeContinuousBodyOnly),
},
{
name: "Low-speed continuous",
input: "Low-speed continuous",
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeLowSpeedContinuous),
},
{
name: "High-speed continuous",
input: "High-speed continuous",
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeHighSpeedContinuous),
},
{
name: "Ultra-high-speed continuous",
input: "Ultra-high-speed continuous",
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeUltraHighSpeedContinuous),
},
{
name: "2-sec. self-timer",
input: "2-sec. self-timer",
expOutput: ptrFilmAdvanceMode(FilmAdvanceMode2secSelfTimer),
},
{
name: "10-sec. self-timer",
input: "10-sec. self-timer",
expOutput: ptrFilmAdvanceMode(FilmAdvanceMode10secSelfTimer),
},
{
name: "Ultra-high-speed continuous with spaces",
input: " Ultra-high-speed continuous ",
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeUltraHighSpeedContinuous),
},
{
name: "some random text",
input: "aksjdfghq3",
expError: errors.New("error parsing FilmAdvanceMode: unknown value `aksjdfghq3`"),
},
{
name: "empty string",
input: "",
expError: errors.New("error parsing FilmAdvanceMode: unknown value ``"),
},
}

for _, tc := range tcs {
sm, err := FilmAdvanceModeFromString(tc.input)
if tc.expError == nil {
r.Equalf(tc.expOutput, sm, tc.name)
r.NoErrorf(err, tc.name)
} else {
r.Errorf(err, tc.name)
r.Equalf(tc.expError.Error(), err.Error(), tc.name)
}
}
}

func ptrFilmAdvanceMode(fam FilmAdvanceMode) *FilmAdvanceMode {
if fam == "" {
return nil
}
return &fam
}
2 changes: 1 addition & 1 deletion types/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Frame struct {
FlashMode *FlashMode
MeteringMode *MeteringMode
ShootingMode *ShootingMode
FilmAdvanceMode *string
FilmAdvanceMode *FilmAdvanceMode
AFMode *AFMode
BulbExposureTime *string
Timestamp *time.Time
Expand Down

0 comments on commit 8c44434

Please sign in to comment.