Rewrite if-else-if-else to switch statements

pull/12/head
Motkov Kirill 2019-03-10 23:22:09 +03:00
parent c20b208309
commit 9dc630677f
4 changed files with 597 additions and 591 deletions

View File

@ -108,12 +108,12 @@ func main() {
var value interface{}
if tagType.Type() == exif.TypeUndefined {
var err error
value, err = exif.UndefinedValue(ifdPath, tagId, valueContext, tagType.ByteOrder())
if log.Is(err, exif.ErrUnhandledUnknownTypedTag) {
switch value, err = exif.UndefinedValue(ifdPath, tagId, valueContext, tagType.ByteOrder()); {
case log.Is(err, exif.ErrUnhandledUnknownTypedTag):
value = nil
} else if err != nil {
case err != nil:
log.Panic(err)
} else {
default:
valueString = fmt.Sprintf("%v", value)
}
} else {

View File

@ -30,7 +30,8 @@ type IfdBuilderTagValue struct {
}
func (ibtv IfdBuilderTagValue) String() string {
if ibtv.IsBytes() == true {
switch {
case ibtv.IsBytes():
var valuePhrase string
if len(ibtv.valueBytes) <= 8 {
valuePhrase = fmt.Sprintf("%v", ibtv.valueBytes)
@ -39,9 +40,9 @@ func (ibtv IfdBuilderTagValue) String() string {
}
return fmt.Sprintf("IfdBuilderTagValue<BYTES=%v LEN=(%d)>", valuePhrase, len(ibtv.valueBytes))
} else if ibtv.IsIb() == true {
case ibtv.IsIb():
return fmt.Sprintf("IfdBuilderTagValue<IB=%s>", ibtv.ib)
} else {
default:
log.Panicf("IBTV state undefined")
return ""
}
@ -567,12 +568,12 @@ func (ib *IfdBuilder) printTagTree(levels int) {
if isChildIb == true {
tagName = "<Child IFD>"
} else {
it, err := ib.tagIndex.Get(tag.ifdPath, tag.tagId)
if log.Is(err, ErrTagNotFound) == true {
switch it, err := ib.tagIndex.Get(tag.ifdPath, tag.tagId); {
case log.Is(err, ErrTagNotFound):
tagName = "<UNKNOWN>"
} else if err != nil {
case err != nil:
log.Panic(err)
} else {
default:
tagName = it.Name
}
}
@ -808,13 +809,13 @@ func (ib *IfdBuilder) Set(bt *BuilderTag) (err error) {
}
}()
position, err := ib.Find(bt.tagId)
if err == nil {
switch position, err := ib.Find(bt.tagId); {
case err == nil:
ib.tags[position] = bt
} else if log.Is(err, ErrTagEntryNotFound) == true {
case log.Is(err, ErrTagEntryNotFound):
err = ib.add(bt)
log.PanicIf(err)
} else {
default:
log.Panic(err)
}
@ -907,11 +908,12 @@ func (ib *IfdBuilder) add(bt *BuilderTag) (err error) {
}
}()
if bt.ifdPath == "" {
switch {
case bt.ifdPath == "":
log.Panicf("BuilderTag ifdPath is not set: %s", bt)
} else if bt.typeId == 0x0 {
case bt.typeId == 0x0:
log.Panicf("BuilderTag type-ID is not set: %s", bt)
} else if bt.value == nil {
case bt.value == nil:
log.Panicf("BuilderTag value is not set: %s", bt)
}

View File

@ -209,8 +209,10 @@ func UndefinedValue(ifdPath string, tagId uint16, valueContext ValueContext, byt
typeLogger.Debugf(nil, "UndefinedValue: IFD-PATH=[%s] TAG-ID=(0x%02x)", ifdPath, tagId)
if ifdPath == IfdPathStandardExif {
if tagId == 0x9000 {
switch ifdPath {
case IfdPathStandardExif:
switch tagId {
case 0x9000:
// ExifVersion
tt := NewTagType(TypeAsciiNoNul, byteOrder)
@ -219,7 +221,7 @@ func UndefinedValue(ifdPath string, tagId uint16, valueContext ValueContext, byt
log.PanicIf(err)
return TagUnknownType_GeneralString(valueString), nil
} else if tagId == 0xa000 {
case 0xa000:
// FlashpixVersion
tt := NewTagType(TypeAsciiNoNul, byteOrder)
@ -228,7 +230,7 @@ func UndefinedValue(ifdPath string, tagId uint16, valueContext ValueContext, byt
log.PanicIf(err)
return TagUnknownType_GeneralString(valueString), nil
} else if tagId == 0x9286 {
case 0x9286:
// UserComment
tt := NewTagType(TypeByte, byteOrder)
@ -255,7 +257,7 @@ func UndefinedValue(ifdPath string, tagId uint16, valueContext ValueContext, byt
typeLogger.Warningf(nil, "User-comment encoding not valid. Returning 'unknown' type (the default).")
return unknownUc, nil
} else if tagId == 0x927c {
case 0x927c:
// MakerNote
// TODO(dustin): !! This is the Wild Wild West. This very well might be a child IFD, but any and all OEM's define their own formats. If we're going to be writing changes and this is complete EXIF (which may not have the first eight bytes), it might be fine. However, if these are just IFDs they'll be relative to the main EXIF, this will invalidate the MakerNote data for IFDs and any other implementations that use offsets unless we can interpret them all. It be best to return to this later and just exclude this from being written for now, though means a loss of a wealth of image metadata.
// -> We can also just blindly try to interpret as an IFD and just validate that it's looks good (maybe it will even have a 'next ifd' pointer that we can validate is 0x0).
@ -284,7 +286,7 @@ func UndefinedValue(ifdPath string, tagId uint16, valueContext ValueContext, byt
}
return mn, nil
} else if tagId == 0x9101 {
case 0x9101:
// ComponentsConfiguration
tt := NewTagType(TypeByte, byteOrder)
@ -310,7 +312,7 @@ func UndefinedValue(ifdPath string, tagId uint16, valueContext ValueContext, byt
return cc, nil
}
} else if ifdPath == IfdPathStandardGps {
case IfdPathStandardGps:
if tagId == 0x001c {
// GPSAreaInformation
@ -330,7 +332,7 @@ func UndefinedValue(ifdPath string, tagId uint16, valueContext ValueContext, byt
return TagUnknownType_GeneralString(valueString), nil
}
} else if ifdPath == IfdPathStandardExifIop {
case IfdPathStandardExifIop:
if tagId == 0x0002 {
// InteropVersion

138
type.go
View File

@ -1,8 +1,8 @@
package exif
import (
"errors"
"bytes"
"errors"
"fmt"
"strconv"
"strings"
@ -31,7 +31,7 @@ var (
)
var (
TypeNames = map[uint16]string {
TypeNames = map[uint16]string{
TypeByte: "BYTE",
TypeAscii: "ASCII",
TypeShort: "SHORT",
@ -44,7 +44,7 @@ var (
TypeAsciiNoNul: "_ASCII_NO_NUL",
}
TypeNamesR = map[string]uint16 {}
TypeNamesR = map[string]uint16{}
)
var (
@ -62,7 +62,6 @@ var (
ErrUnhandledUnknownTypedTag = errors.New("not a standard unknown-typed tag")
)
type Rational struct {
Numerator uint32
Denominator uint32
@ -79,7 +78,6 @@ func init() {
}
}
type TagType struct {
tagType uint16
name string
@ -120,21 +118,20 @@ func (tt TagType) Size() int {
}
func TagTypeSize(tagType uint16) int {
if tagType == TypeByte {
switch tagType {
case TypeByte, TypeAscii, TypeAsciiNoNul:
return 1
} else if tagType == TypeAscii || tagType == TypeAsciiNoNul {
return 1
} else if tagType == TypeShort {
case TypeShort:
return 2
} else if tagType == TypeLong {
case TypeLong:
return 4
} else if tagType == TypeRational {
case TypeRational:
return 8
} else if tagType == TypeSignedLong {
case TypeSignedLong:
return 4
} else if tagType == TypeSignedRational {
case TypeSignedRational:
return 8
} else {
default:
log.Panicf("can not determine tag-value size for type (%d): [%s]", tagType, TypeNames[tagType])
// Never called.
@ -188,7 +185,7 @@ func (tt TagType) ParseAscii(data []byte, unitCount uint32) (value string, err e
log.Panic(ErrNotEnoughData)
}
if len(data) == 0 || data[count - 1] != 0 {
if len(data) == 0 || data[count-1] != 0 {
s := string(data[:count])
typeLogger.Warningf(nil, "ascii not terminated with nul as expected: [%v]", s)
@ -197,7 +194,7 @@ func (tt TagType) ParseAscii(data []byte, unitCount uint32) (value string, err e
// Auto-strip the NUL from the end. It serves no purpose outside of
// encoding semantics.
return string(data[:count - 1]), nil
return string(data[:count-1]), nil
}
}
@ -302,10 +299,10 @@ func (tt TagType) ParseRationals(data []byte, unitCount uint32) (value []Rationa
for i := 0; i < count; i++ {
if tt.byteOrder == binary.BigEndian {
value[i].Numerator = binary.BigEndian.Uint32(data[i*8:])
value[i].Denominator = binary.BigEndian.Uint32(data[i*8 + 4:])
value[i].Denominator = binary.BigEndian.Uint32(data[i*8+4:])
} else {
value[i].Numerator = binary.LittleEndian.Uint32(data[i*8:])
value[i].Denominator = binary.LittleEndian.Uint32(data[i*8 + 4:])
value[i].Denominator = binary.LittleEndian.Uint32(data[i*8+4:])
}
}
@ -578,56 +575,58 @@ func (tt TagType) ResolveAsString(valueContext ValueContext, justFirst bool) (va
}
}()
// TODO(dustin): Implement Resolve(), below.
// TODO(dustin): Implement Resolve(), below.
// valueRaw, err := tt.Resolve(valueContext)
// log.PanicIf(err)
typeId := tt.Type()
if typeId == TypeByte {
switch typeId := tt.Type(); typeId {
case TypeByte:
raw, err := tt.ReadByteValues(valueContext)
log.PanicIf(err)
if justFirst == false {
switch {
case justFirst == false:
return DumpBytesToString(raw), nil
} else if valueContext.UnitCount > 0 {
case valueContext.UnitCount > 0:
return fmt.Sprintf("0x%02x", raw[0]), nil
} else {
default:
return "", nil
}
} else if typeId == TypeAscii {
case TypeAscii:
raw, err := tt.ReadAsciiValue(valueContext)
log.PanicIf(err)
return fmt.Sprintf("%s", raw), nil
} else if typeId == TypeAsciiNoNul {
case TypeAsciiNoNul:
raw, err := tt.ReadAsciiNoNulValue(valueContext)
log.PanicIf(err)
return fmt.Sprintf("%s", raw), nil
} else if typeId == TypeShort {
case TypeShort:
raw, err := tt.ReadShortValues(valueContext)
log.PanicIf(err)
if justFirst == false {
switch {
case justFirst == false:
return fmt.Sprintf("%v", raw), nil
} else if valueContext.UnitCount > 0 {
case valueContext.UnitCount > 0:
return fmt.Sprintf("%v", raw[0]), nil
} else {
default:
return "", nil
}
} else if typeId == TypeLong {
case TypeLong:
raw, err := tt.ReadLongValues(valueContext)
log.PanicIf(err)
if justFirst == false {
switch {
case justFirst == false:
return fmt.Sprintf("%v", raw), nil
} else if valueContext.UnitCount > 0 {
case valueContext.UnitCount > 0:
return fmt.Sprintf("%v", raw[0]), nil
} else {
default:
return "", nil
}
} else if typeId == TypeRational {
case TypeRational:
raw, err := tt.ReadRationalValues(valueContext)
log.PanicIf(err)
@ -636,25 +635,27 @@ func (tt TagType) ResolveAsString(valueContext ValueContext, justFirst bool) (va
parts[i] = fmt.Sprintf("%d/%d", r.Numerator, r.Denominator)
}
if justFirst == false {
switch {
case justFirst == false:
return fmt.Sprintf("%v", parts), nil
} else if valueContext.UnitCount > 0 {
case valueContext.UnitCount > 0:
return parts[0], nil
} else {
default:
return "", nil
}
} else if typeId == TypeSignedLong {
case TypeSignedLong:
raw, err := tt.ReadSignedLongValues(valueContext)
log.PanicIf(err)
if justFirst == false {
switch {
case justFirst == false:
return fmt.Sprintf("%v", raw), nil
} else if valueContext.UnitCount > 0 {
case valueContext.UnitCount > 0:
return fmt.Sprintf("%v", raw[0]), nil
} else {
default:
return "", nil
}
} else if typeId == TypeSignedRational {
case TypeSignedRational:
raw, err := tt.ReadSignedRationalValues(valueContext)
log.PanicIf(err)
@ -663,14 +664,15 @@ func (tt TagType) ResolveAsString(valueContext ValueContext, justFirst bool) (va
parts[i] = fmt.Sprintf("%d/%d", r.Numerator, r.Denominator)
}
if justFirst == false {
switch {
case justFirst == false:
return fmt.Sprintf("%v", raw), nil
} else if valueContext.UnitCount > 0 {
case valueContext.UnitCount > 0:
return parts[0], nil
} else {
default:
return "", nil
}
} else {
default:
log.Panicf("value of type (%d) [%s] is unparseable", typeId, tt)
// Never called.
@ -690,38 +692,37 @@ func (tt TagType) Resolve(valueContext ValueContext) (value interface{}, err err
}
}()
typeId := tt.Type()
if typeId == TypeByte {
switch typeId := tt.Type(); typeId {
case TypeByte:
value, err = tt.ReadByteValues(valueContext)
log.PanicIf(err)
} else if typeId == TypeAscii {
case TypeAscii:
value, err = tt.ReadAsciiValue(valueContext)
log.PanicIf(err)
} else if typeId == TypeAsciiNoNul {
case TypeAsciiNoNul:
value, err = tt.ReadAsciiNoNulValue(valueContext)
log.PanicIf(err)
} else if typeId == TypeShort {
case TypeShort:
value, err = tt.ReadShortValues(valueContext)
log.PanicIf(err)
} else if typeId == TypeLong {
case TypeLong:
value, err = tt.ReadLongValues(valueContext)
log.PanicIf(err)
} else if typeId == TypeRational {
case TypeRational:
value, err = tt.ReadRationalValues(valueContext)
log.PanicIf(err)
} else if typeId == TypeSignedLong {
case TypeSignedLong:
value, err = tt.ReadSignedLongValues(valueContext)
log.PanicIf(err)
} else if typeId == TypeSignedRational {
case TypeSignedRational:
value, err = tt.ReadSignedRationalValues(valueContext)
log.PanicIf(err)
} else if typeId == TypeUndefined {
case TypeUndefined:
log.Panicf("will not parse unknown-type value: %v", tt)
// Never called.
return nil, nil
} else {
default:
log.Panicf("value of type (%d) [%s] is unparseable", typeId, tt)
// Never called.
@ -755,29 +756,30 @@ func (tt TagType) FromString(valueString string) (value interface{}, err error)
}()
if tt.tagType == TypeUndefined {
// TODO(dustin): Circle back to this.
// TODO(dustin): Circle back to this.
log.Panicf("undefined-type values are not supported")
}
if tt.tagType == TypeByte {
switch tt.tagType {
case TypeByte:
return []byte(valueString), nil
} else if tt.tagType == TypeAscii || tt.tagType == TypeAsciiNoNul {
case TypeAscii, TypeAsciiNoNul:
// Whether or not we're putting an NUL on the end is only relevant for
// byte-level encoding. This function really just supports a user
// interface.
return valueString, nil
} else if tt.tagType == TypeShort {
case TypeShort:
n, err := strconv.ParseUint(valueString, 10, 16)
log.PanicIf(err)
return uint16(n), nil
} else if tt.tagType == TypeLong {
case TypeLong:
n, err := strconv.ParseUint(valueString, 10, 32)
log.PanicIf(err)
return uint32(n), nil
} else if tt.tagType == TypeRational {
case TypeRational:
parts := strings.SplitN(valueString, "/", 2)
numerator, err := strconv.ParseUint(parts[0], 10, 32)
@ -790,12 +792,12 @@ func (tt TagType) FromString(valueString string) (value interface{}, err error)
Numerator: uint32(numerator),
Denominator: uint32(denominator),
}, nil
} else if tt.tagType == TypeSignedLong {
case TypeSignedLong:
n, err := strconv.ParseInt(valueString, 10, 32)
log.PanicIf(err)
return int32(n), nil
} else if tt.tagType == TypeSignedRational {
case TypeSignedRational:
parts := strings.SplitN(valueString, "/", 2)
numerator, err := strconv.ParseInt(parts[0], 10, 32)