@@ -25,32 +25,57 @@ import (
2525)
2626
2727const (
28- // CurrentVersion is the current vesion of the CDI Spec.
28+ // CurrentVersion is the current version of the CDI Spec.
2929 CurrentVersion = cdi .CurrentVersion
3030
31+ // vCurrent is the current version as a semver-comparable type
3132 vCurrent version = "v" + CurrentVersion
3233
33- vEarliest version = v020
34-
34+ // These represent the released versions of the CDI specification
35+ v010 version = "v0.1.0"
3536 v020 version = "v0.2.0"
3637 v030 version = "v0.3.0"
3738 v040 version = "v0.4.0"
3839 v050 version = "v0.5.0"
40+
41+ // vEarliest is the earliest supported version of the CDI specification
42+ vEarliest version = v020
3943)
4044
45+ // validSpecVersions stores a map of spec versions to functions to check the required versions.
46+ // Adding new fields / spec versions requires that a `requiredFunc` be implemented and
47+ // this map be updated.
48+ var validSpecVersions = requiredVersionMap {
49+ v010 : nil ,
50+ v020 : nil ,
51+ v030 : nil ,
52+ v040 : requiresV040 ,
53+ v050 : requiresV050 ,
54+ }
55+
56+ // MinimumRequiredVersion determines the minumum spec version for the input spec.
57+ func MinimumRequiredVersion (spec * cdi.Spec ) (string , error ) {
58+ minVersion := validSpecVersions .requiredVersion (spec )
59+ return minVersion .String (), nil
60+ }
61+
4162// version represents a semantic version string
4263type version string
4364
65+ // newVersion creates a version that can be used for semantic version comparisons.
66+ func newVersion (v string ) version {
67+ return version ("v" + strings .TrimPrefix (v , "v" ))
68+ }
69+
4470// String returns the string representation of the version.
4571// This trims a leading v if present.
4672func (v version ) String () string {
4773 return strings .TrimPrefix (string (v ), "v" )
4874}
4975
50- // LT checks whether a version is less than the specified version.
51- // Semantic versioning is used to perform the comparison.
52- func (v version ) LT (o version ) bool {
53- return semver .Compare (string (v ), string (o )) < 0
76+ // IsGreaterThan checks with a version is greater than the specified version.
77+ func (v version ) IsGreaterThan (o version ) bool {
78+ return semver .Compare (string (v ), string (o )) > 0
5479}
5580
5681// IsLatest checks whether the version is the latest supported version
@@ -62,24 +87,24 @@ type requiredFunc func(*cdi.Spec) bool
6287
6388type requiredVersionMap map [version ]requiredFunc
6489
65- // required stores a map of spec versions to functions to check the required versions .
66- // Adding new fields / spec versions requires that a `requiredFunc` be implemented and
67- // this map be updated.
68- var required = requiredVersionMap {
69- v050 : requiresV050 ,
70- v040 : requiresV040 ,
90+ // isValidVersion checks whether the specified version is valid .
91+ // A version is valid if it is contained in the required version map.
92+ func ( r requiredVersionMap ) isValidVersion ( specVersion string ) bool {
93+ _ , ok := validSpecVersions [ newVersion ( specVersion )]
94+
95+ return ok
7196}
7297
73- // minVersion returns the minimum version required for the given spec
74- func (r requiredVersionMap ) minVersion (spec * cdi.Spec ) version {
98+ // requiredVersion returns the minimum version required for the given spec
99+ func (r requiredVersionMap ) requiredVersion (spec * cdi.Spec ) version {
75100 minVersion := vEarliest
76101
77- for specVersion := range validSpecVersions {
78- v := version ( "v" + strings . TrimPrefix ( specVersion , "v" ))
79- if f , ok := r [ v ]; ok {
80- if f ( spec ) && minVersion . LT ( v ) {
81- minVersion = v
82- }
102+ for v , isRequired := range validSpecVersions {
103+ if isRequired == nil {
104+ continue
105+ }
106+ if isRequired ( spec ) && v . IsGreaterThan ( minVersion ) {
107+ minVersion = v
83108 }
84109 // If we have already detected the latest version then no later version could be detected
85110 if minVersion .IsLatest () {
0 commit comments