summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/rwcarlsen/goexif/exif/regen_regress.go
blob: 17bac52875b11a8ff1115a7a2096c68da7c7ff1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// +build ignore

package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/rwcarlsen/goexif/exif"
	"github.com/rwcarlsen/goexif/tiff"
)

func main() {
	flag.Parse()
	fname := flag.Arg(0)

	dst, err := os.Create(fname)
	if err != nil {
		log.Fatal(err)
	}
	defer dst.Close()

	dir, err := os.Open("samples")
	if err != nil {
		log.Fatal(err)
	}
	defer dir.Close()

	names, err := dir.Readdirnames(0)
	if err != nil {
		log.Fatal(err)
	}
	for i, name := range names {
		names[i] = filepath.Join("samples", name)
	}
	makeExpected(names, dst)
}

func makeExpected(files []string, w io.Writer) {
	fmt.Fprintf(w, "package exif\n\n")
	fmt.Fprintf(w, "var regressExpected = map[string]map[FieldName]string{\n")

	for _, name := range files {
		f, err := os.Open(name)
		if err != nil {
			continue
		}

		x, err := exif.Decode(f)
		if err != nil {
			f.Close()
			continue
		}

		fmt.Fprintf(w, "\"%v\": map[FieldName]string{\n", filepath.Base(name))
		x.Walk(&regresswalk{w})
		fmt.Fprintf(w, "},\n")
		f.Close()
	}
	fmt.Fprintf(w, "}")
}

type regresswalk struct {
	wr io.Writer
}

func (w *regresswalk) Walk(name exif.FieldName, tag *tiff.Tag) error {
	if strings.HasPrefix(string(name), exif.UnknownPrefix) {
		fmt.Fprintf(w.wr, "\"%v\": `%v`,\n", name, tag.String())
	} else {
		fmt.Fprintf(w.wr, "%v: `%v`,\n", name, tag.String())
	}
	return nil
}