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

Commit

Permalink
DRY ptr* functions
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Shishkin <[email protected]>
  • Loading branch information
teran committed Jan 5, 2020
1 parent 86838c1 commit 4817dc6
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 389 deletions.
50 changes: 19 additions & 31 deletions exiftool/exiftool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,25 @@ func TestFromFrame(t *testing.T) {
{
name: "filled frame",
frame: types.Frame{
Flag: ptrBool(true),
Number: ptrInt64(23),
FocalLength: ptrInt64(123),
MaxAperture: ptrFloat64(1.4),
Tv: ptrString("1/300"),
Av: ptrFloat64(1.5),
ISO: ptrInt64(400),
ExposureCompensation: ptrFloat64(3.2),
FlashCompensation: ptrFloat64(-3.2),
FlashMode: ptrFlashMode(types.FlashModeOff),
MeteringMode: ptrMeteringMode(types.MeteringModeEvaluative),
ShootingMode: ptrShootingMode(types.ShootingModeProgramAE),
FilmAdvanceMode: ptrFilmAdvanceMode(types.FilmAdvanceModeSingleFrame),
AFMode: ptrAFMode(types.AFModeOneShotAF),
BulbExposureTime: ptrString("1/300"),
Timestamp: ptrTime(time.Date(2009, 2, 12, 15, 34, 23, 0, time.UTC)),
MultipleExposure: ptrMultipleExposure(types.MultipleExposureOff),
BatteryLoadedDate: ptrTime(time.Date(2008, 1, 5, 11, 58, 14, 0, time.UTC)),
Remarks: ptrString("blah!"),
Flag: types.PtrBool(true),
Number: types.PtrInt64(23),
FocalLength: types.PtrInt64(123),
MaxAperture: types.PtrFloat64(1.4),
Tv: types.PtrString("1/300"),
Av: types.PtrFloat64(1.5),
ISO: types.PtrInt64(400),
ExposureCompensation: types.PtrFloat64(3.2),
FlashCompensation: types.PtrFloat64(-3.2),
FlashMode: types.PtrFlashMode(types.FlashModeOff),
MeteringMode: types.PtrMeteringMode(types.MeteringModeEvaluative),
ShootingMode: types.PtrShootingMode(types.ShootingModeProgramAE),
FilmAdvanceMode: types.PtrFilmAdvanceMode(types.FilmAdvanceModeSingleFrame),
AFMode: types.PtrAFMode(types.AFModeOneShotAF),
BulbExposureTime: types.PtrString("1/300"),
Timestamp: types.PtrTime(time.Date(2009, 2, 12, 15, 34, 23, 0, time.UTC)),
MultipleExposure: types.PtrMultipleExposure(types.MultipleExposureOff),
BatteryLoadedDate: types.PtrTime(time.Date(2008, 1, 5, 11, 58, 14, 0, time.UTC)),
Remarks: types.PtrString("blah!"),
},
expOptions: []ExifToolOption{
{
Expand Down Expand Up @@ -301,15 +301,3 @@ func TestFromFrame(t *testing.T) {
r.ElementsMatchf(tc.expOptions, e.Options(), tc.name)
}
}

func ptrBool(t bool) *bool { return &t }
func ptrInt64(t int64) *int64 { return &t }
func ptrFloat64(t float64) *float64 { return &t }
func ptrString(t string) *string { return &t }
func ptrTime(t time.Time) *time.Time { return &t }
func ptrFlashMode(t types.FlashMode) *types.FlashMode { return &t }
func ptrMeteringMode(t types.MeteringMode) *types.MeteringMode { return &t }
func ptrShootingMode(t types.ShootingMode) *types.ShootingMode { return &t }
func ptrFilmAdvanceMode(t types.FilmAdvanceMode) *types.FilmAdvanceMode { return &t }
func ptrAFMode(t types.AFMode) *types.AFMode { return &t }
func ptrMultipleExposure(t types.MultipleExposure) *types.MultipleExposure { return &t }
531 changes: 260 additions & 271 deletions parser/csv_parser_test.go

Large diffs are not rendered by default.

15 changes: 4 additions & 11 deletions types/af_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ func TestAFMode(t *testing.T) {
{
name: "One-Shot AF",
input: "One-Shot AF",
expOutput: ptrAFMode(AFModeOneShotAF),
expOutput: PtrAFMode(AFModeOneShotAF),
expEXIFValue: EXIFValue{
"Canon:FocusMode": "One-Shot AF",
},
},
{
name: "AI Servo AF",
input: "AI Servo AF",
expOutput: ptrAFMode(AFModeAIServoAF),
expOutput: PtrAFMode(AFModeAIServoAF),
expEXIFValue: EXIFValue{
"Canon:FocusMode": "AI Servo AF",
},
},
{
name: "Manual focus",
input: "Manual focus",
expOutput: ptrAFMode(AFModeManualFocus),
expOutput: PtrAFMode(AFModeManualFocus),
expEXIFValue: EXIFValue{
"Canon:FocusMode": "Manual focus",
},
},
{
name: "Manual focus with spaces",
input: " Manual focus ",
expOutput: ptrAFMode(AFModeManualFocus),
expOutput: PtrAFMode(AFModeManualFocus),
expEXIFValue: EXIFValue{
"Canon:FocusMode": "Manual focus",
},
Expand Down Expand Up @@ -75,10 +75,3 @@ func TestAFMode(t *testing.T) {
}
}
}

func ptrAFMode(am AFMode) *AFMode {
if am == "" {
return nil
}
return &am
}
23 changes: 8 additions & 15 deletions types/film_advance_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,42 @@ func TestFilmAdvanceMode(t *testing.T) {
{
name: "Single-frame",
input: "Single-frame",
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeSingleFrame),
expOutput: PtrFilmAdvanceMode(FilmAdvanceModeSingleFrame),
},
{
name: "Continuous (body only)",
input: "Continuous (body only)",
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeContinuousBodyOnly),
expOutput: PtrFilmAdvanceMode(FilmAdvanceModeContinuousBodyOnly),
},
{
name: "Low-speed continuous",
input: "Low-speed continuous",
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeLowSpeedContinuous),
expOutput: PtrFilmAdvanceMode(FilmAdvanceModeLowSpeedContinuous),
},
{
name: "High-speed continuous",
input: "High-speed continuous",
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeHighSpeedContinuous),
expOutput: PtrFilmAdvanceMode(FilmAdvanceModeHighSpeedContinuous),
},
{
name: "Ultra-high-speed continuous",
input: "Ultra-high-speed continuous",
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeUltraHighSpeedContinuous),
expOutput: PtrFilmAdvanceMode(FilmAdvanceModeUltraHighSpeedContinuous),
},
{
name: "2-sec. self-timer",
input: "2-sec. self-timer",
expOutput: ptrFilmAdvanceMode(FilmAdvanceMode2secSelfTimer),
expOutput: PtrFilmAdvanceMode(FilmAdvanceMode2secSelfTimer),
},
{
name: "10-sec. self-timer",
input: "10-sec. self-timer",
expOutput: ptrFilmAdvanceMode(FilmAdvanceMode10secSelfTimer),
expOutput: PtrFilmAdvanceMode(FilmAdvanceMode10secSelfTimer),
},
{
name: "Ultra-high-speed continuous with spaces",
input: " Ultra-high-speed continuous ",
expOutput: ptrFilmAdvanceMode(FilmAdvanceModeUltraHighSpeedContinuous),
expOutput: PtrFilmAdvanceMode(FilmAdvanceModeUltraHighSpeedContinuous),
},
{
name: "some random text",
Expand All @@ -81,10 +81,3 @@ func TestFilmAdvanceMode(t *testing.T) {
}
}
}

func ptrFilmAdvanceMode(fam FilmAdvanceMode) *FilmAdvanceMode {
if fam == "" {
return nil
}
return &fam
}
18 changes: 7 additions & 11 deletions types/film_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,37 @@ func TestFilmIsEmpty(t *testing.T) {
tcs := []testCase{
{
name: "id filled",
filmSample: Film{ID: ptrInt64(1234)},
filmSample: Film{ID: PtrInt64(1234)},
expResult: false,
},
{
name: "camera id filled",
filmSample: Film{CameraID: ptrInt64(1234)},
filmSample: Film{CameraID: PtrInt64(1234)},
expResult: false,
},
{
name: "title filled",
filmSample: Film{Title: ptrString("test titme")},
filmSample: Film{Title: PtrString("test titme")},
expResult: false,
},
{
name: "timestamp filled",
filmSample: Film{FilmLoadedTimestamp: ptrTime(time.Now())},
filmSample: Film{FilmLoadedTimestamp: PtrTime(time.Now())},
expResult: false,
},
{
name: "frame count filled",
filmSample: Film{FrameCount: ptrInt64(1234)},
filmSample: Film{FrameCount: PtrInt64(1234)},
expResult: false,
},
{
name: "iso filled",
filmSample: Film{ISO: ptrInt64(1234)},
filmSample: Film{ISO: PtrInt64(1234)},
expResult: false,
},
{
name: "string filled",
filmSample: Film{Remarks: ptrString("blah")},
filmSample: Film{Remarks: PtrString("blah")},
expResult: false,
},
{
Expand All @@ -68,7 +68,3 @@ func TestFilmIsEmpty(t *testing.T) {
r.Equalf(tc.expResult, tc.filmSample.IsEmpty(), tc.name)
}
}

func ptrInt64(t int64) *int64 { return &t }
func ptrString(t string) *string { return &t }
func ptrTime(t time.Time) *time.Time { return &t }
21 changes: 7 additions & 14 deletions types/flash_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestFlashMode(t *testing.T) {
{
name: "ON",
input: "ON",
expOutput: ptrFlashMode(FlashModeOn),
expOutput: PtrFlashMode(FlashModeOn),
expEXIFValue: EXIFValue{
"Canon:CanonFlashMode": "On",
"Canon:FlashBits": "(none)",
Expand All @@ -32,7 +32,7 @@ func TestFlashMode(t *testing.T) {
{
name: "OFF",
input: "OFF",
expOutput: ptrFlashMode(FlashModeOff),
expOutput: PtrFlashMode(FlashModeOff),
expEXIFValue: EXIFValue{
"Canon:CanonFlashMode": "Off",
"Canon:FlashBits": "(none)",
Expand All @@ -42,7 +42,7 @@ func TestFlashMode(t *testing.T) {
{
name: "E-TTL",
input: "E-TTL",
expOutput: ptrFlashMode(FlashModeETTL),
expOutput: PtrFlashMode(FlashModeETTL),
expEXIFValue: EXIFValue{
"Canon:CanonFlashMode": "On",
"Canon:FlashBits": "E-TTL",
Expand All @@ -52,7 +52,7 @@ func TestFlashMode(t *testing.T) {
{
name: "A-TTL",
input: "A-TTL",
expOutput: ptrFlashMode(FlashModeATTL),
expOutput: PtrFlashMode(FlashModeATTL),
expEXIFValue: EXIFValue{
"Canon:CanonFlashMode": "On",
"Canon:FlashBits": "A-TTL",
Expand All @@ -62,7 +62,7 @@ func TestFlashMode(t *testing.T) {
{
name: "TTL autoflash",
input: "TTL autoflash",
expOutput: ptrFlashMode(FlashModeTTLAutoflash),
expOutput: PtrFlashMode(FlashModeTTLAutoflash),
expEXIFValue: EXIFValue{
"Canon:CanonFlashMode": "Auto",
"Canon:FlashBits": "TTL",
Expand All @@ -72,7 +72,7 @@ func TestFlashMode(t *testing.T) {
{
name: "Manual flash",
input: "Manual flash",
expOutput: ptrFlashMode(FlashModeManualFlash),
expOutput: PtrFlashMode(FlashModeManualFlash),
expEXIFValue: EXIFValue{
"Canon:CanonFlashMode": "On",
"Canon:FlashBits": "Manual",
Expand All @@ -82,7 +82,7 @@ func TestFlashMode(t *testing.T) {
{
name: "TTL autoflash with spaces",
input: " TTL autoflash ",
expOutput: ptrFlashMode(FlashModeTTLAutoflash),
expOutput: PtrFlashMode(FlashModeTTLAutoflash),
expEXIFValue: EXIFValue{
"Canon:CanonFlashMode": "Auto",
"Canon:FlashBits": "TTL",
Expand Down Expand Up @@ -113,10 +113,3 @@ func TestFlashMode(t *testing.T) {
}
}
}

func ptrFlashMode(fm FlashMode) *FlashMode {
if fm == "" {
return nil
}
return &fm
}
66 changes: 66 additions & 0 deletions types/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package types

import "time"

// PtrBool ...
func PtrBool(t bool) *bool { return &t }

// PtrInt64 ...
func PtrInt64(t int64) *int64 { return &t }

// PtrFloat64 ...
func PtrFloat64(t float64) *float64 { return &t }

// PtrString ...
func PtrString(t string) *string { return &t }

// PtrTime ...
func PtrTime(t time.Time) *time.Time { return &t }

// PtrFilmAdvanceMode ...
func PtrFilmAdvanceMode(fam FilmAdvanceMode) *FilmAdvanceMode {
if fam == "" {
return nil
}
return &fam
}

// PtrAFMode ...
func PtrAFMode(t AFMode) *AFMode {
if t == "" {
return nil
}
return &t
}

// PtrFlashMode ...
func PtrFlashMode(fm FlashMode) *FlashMode {
if fm == "" {
return nil
}
return &fm
}

// PtrMeteringMode ...
func PtrMeteringMode(mm MeteringMode) *MeteringMode {
if mm == "" {
return nil
}
return &mm
}

// PtrMultipleExposure ...
func PtrMultipleExposure(me MultipleExposure) *MultipleExposure {
if me == "" {
return nil
}
return &me
}

// PtrShootingMode ...
func PtrShootingMode(sm ShootingMode) *ShootingMode {
if sm == "" {
return nil
}
return &sm
}
Loading

0 comments on commit 4817dc6

Please sign in to comment.