Skip to content

🏭 Vector Factory ​

pkg/vector/factory.go · 1 dispatcher + 23 typed factories

Synopsis ​

The factory layer turns raw short names (AV, E, MAV, …) and single-character value codes ('N', 'F', 'X', …) into typed vector.Vector presets. GetVectorByShortName is the generic dispatcher used by the parser; the 23 Get* functions are the per-metric entry points it delegates to (and that callers can use directly).

go
v, err := vector.GetVectorByShortName("AV", "N") // -> AttackVectorNetwork, nil
v, err = vector.GetAttackVector('N')              // same, via the typed factory

How It Works ​

GetVectorByShortName validates the value is a single character, then switches on the short name to delegate to one of 23 typed factories. Each typed factory switches on the value rune and returns the matching package-level preset variable; an unrecognized rune yields a typed error.

API Reference ​

GetVectorByShortName ​

go
func GetVectorByShortName(shortName string, value string) (Vector, error)

Generic dispatcher. value must be a single character (otherwise invalid vector value: ...). It switches on shortName and delegates to the matching Get* function:

shortNameDelegates to
AVGetAttackVector
ACGetAttackComplexity
PRGetPrivilegesRequired
UIGetUserInteraction
SGetScope
CGetConfidentiality
IGetIntegrity
AGetAvailability
EGetExploitCodeMaturity
RLGetRemediationLevel
RCGetReportConfidence
CRGetConfidentialityRequirement
IRGetIntegrityRequirement
ARGetAvailabilityRequirement
MAVGetModifiedAttackVector
MACGetModifiedAttackComplexity
MPRGetModifiedPrivilegesRequired
MUIGetModifiedUserInteraction
MSGetModifiedScope
MCGetModifiedConfidentiality
MIGetModifiedIntegrity
MAGetModifiedAvailability

An unknown shortName yields unknown vector short name: ....

Get* functions (23 total) ​

Each typed factory has the same shape:

go
func Get<Metric>(shortValue rune) (Vector, error)

It switches on shortValue and returns the matching preset variable, or unknown <metric> value: %c for anything else. The complete list, grouped by segment:

Base metrics (8)

FunctionLegal values
GetAttackVectorN A L P
GetAttackComplexityL H
GetPrivilegesRequiredN L H
GetUserInteractionN R
GetScopeU C
GetConfidentialityN L H
GetIntegrityN L H
GetAvailabilityN L H

Temporal metrics (3)

FunctionLegal values
GetExploitCodeMaturityX U P F H
GetRemediationLevelX O T W U
GetReportConfidenceX U R C

Environmental requirements (3)

FunctionLegal values
GetConfidentialityRequirementX L M H
GetIntegrityRequirementX L M H
GetAvailabilityRequirementX L M H

Modified base metrics (8)

FunctionLegal values
GetModifiedAttackVectorX N A L P
GetModifiedAttackComplexityX L H
GetModifiedPrivilegesRequiredX N L H
GetModifiedUserInteractionX N R
GetModifiedScopeX U C
GetModifiedConfidentialityX N L H
GetModifiedIntegrityX N L H
GetModifiedAvailabilityX N L H

Note: every modified-metric factory treats X as the Not Defined fallback (see /sdk/vector-not-defined) and returns the corresponding *NotDefined preset rather than a Modified* preset.

Example ​

go
package main

import (
	"fmt"

	"github.com/scagogogo/cvss-skills/pkg/vector"
)

func main() {
	// Via the generic dispatcher (what the parser uses).
	av, err := vector.GetVectorByShortName("AV", "N")
	if err != nil {
		panic(err)
	}
	fmt.Println(av.String()) // AV:N

	// Via the typed factory directly.
	e, err := vector.GetExploitCodeMaturity('F')
	if err != nil {
		panic(err)
	}
	fmt.Println(e.String()) // E:F

	// Not Defined fallback on a modified metric.
	mav, _ := vector.GetModifiedAttackVector('X')
	fmt.Println(mav.String(), mav.IsNotDefined()) // MAV:X true

	// Illegal value.
	_, err = vector.GetScope('Z')
	fmt.Println(err) // unknown scope value: Z
}

Released under the MIT License.