File size: 4,256 Bytes
b110593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: [email protected]
//

//go:build ignore
// +build ignore

// The following directive is necessary to make the package coherent:
// This program generates data.go.
//
//

package main

import (
	"fmt"
	"log"
	"os"
	"text/template"
	"time"

	"github.com/go-openapi/strfmt"
	"github.com/weaviate/weaviate/entities/models"
	"gopkg.in/yaml.v2"
)

func main() {
	fmt.Println("Generating deprecations code")

	fd, err := os.Open("deprecations.yml")
	fatal(err)
	defer fd.Close()

	var deprecations struct {
		// yaml tags not working on the go-swagger model, so we need to do the
		// map[string]interface{} workaround
		Deprecations []map[string]interface{} `yaml:"deprecations"`
	}
	err = yaml.NewDecoder(fd).Decode(&deprecations)
	fatal(err)

	parsed, err := parseDeprecations(deprecations.Deprecations)
	fatal(err)

	f, err := os.Create("data.go")
	fatal(err)
	defer f.Close()
	err = packageTemplate.Execute(f, struct {
		Deprecations []models.Deprecation
	}{
		Deprecations: parsed,
	})
	fatal(err)
}

func parseDeprecations(in []map[string]interface{}) ([]models.Deprecation, error) {
	out := make([]models.Deprecation, len(in))

	for i, d := range in {
		out[i] = models.Deprecation{
			ID:                    d["id"].(string),
			Status:                d["status"].(string),
			APIType:               d["apiType"].(string),
			Msg:                   d["msg"].(string),
			Mitigation:            d["mitigation"].(string),
			SinceVersion:          d["sinceVersion"].(string),
			PlannedRemovalVersion: d["plannedRemovalVersion"].(string),
			Locations:             parseStringSlice(d["locations"].([]interface{})),
			SinceTime:             timeMust(time.Parse(time.RFC3339, d["sinceTime"].(string))),
		}

		if t, ok := d["removedTime"]; ok && t != nil {
			parsed := timeMust(time.Parse(time.RFC3339, t.(string)))
			out[i].RemovedTime = &parsed
		}

		if v, ok := d["removedIn"]; ok && v != nil {
			parsed := v.(string)
			out[i].RemovedIn = &parsed
		}
	}

	return out, nil
}

func timeMust(t time.Time, err error) strfmt.DateTime {
	if err != nil {
		panic(err)
	}

	return strfmt.DateTime(t)
}

func parseStringSlice(in []interface{}) []string {
	out := make([]string, len(in))
	for i, elem := range in {
		out[i] = elem.(string)
	}

	return out
}

func fatal(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

var packageTemplate = template.Must(template.New("").Funcs(
	template.FuncMap{
		"DerefString": func(i *string) string { return *i },
	},
).Parse(`// Code generated by go generate; DO NOT EDIT.

// This file was generated by go generate ./deprecations

package deprecations



import (

	"time"



	"github.com/go-openapi/strfmt"

	"github.com/weaviate/weaviate/entities/models"

)



func timeMust(t time.Time, err error) strfmt.DateTime {

	if err != nil {

		panic(err)

	}



	return strfmt.DateTime(t)

}



func timeMustPtr(t time.Time, err error) *strfmt.DateTime {

	if err != nil {

		panic(err)

	}



	parsed := strfmt.DateTime(t)

	return &parsed

}



func ptString(in string) *string {

	return &in

}



var ByID = map[string]models.Deprecation{

{{- range .Deprecations }}

	{{ printf "%q" .ID }}: {

		ID: {{ printf "%q" .ID }},

		Locations: []string{

		{{- range $index, $element := .Locations }}

			{{ printf "%q," $element }}

		{{- end }}

		},

		Status: {{ printf "%q" .Status }},

		APIType: {{ printf "%q" .APIType }},

		Mitigation: {{ printf "%q" .Mitigation }},

		Msg: {{ printf "%q" .Msg }},

		SinceVersion: {{ printf "%q" .SinceVersion }},

		SinceTime: timeMust(time.Parse(time.RFC3339, {{ printf "%q" .SinceTime }})),

		{{ if .RemovedIn -}}

		RemovedIn: ptString({{ printf "%q" (DerefString .RemovedIn) }}),

		{{- end }}

		{{ if .RemovedTime -}}

		RemovedTime: timeMustPtr(time.Parse(time.RFC3339, {{ printf "%q" .SinceTime }})),

		{{- end }}

	},

{{- end }}

}

`))