vorteil/direktiv

View on GitHub
pkg/model/function-reusable.go

Summary

Maintainability
A
35 mins
Test Coverage
package model

import (
    "errors"
    "fmt"

    "github.com/direktiv/direktiv/pkg/core"
    "github.com/direktiv/direktiv/pkg/utils"
)

// ReusableFunctionDefinition defines a reusable function and the fields it requires.
type ReusableFunctionDefinition struct {
    Type    FunctionType               `json:"type"              yaml:"type"`
    ID      string                     `json:"id"                yaml:"id"`
    Image   string                     `json:"image"             yaml:"image"`
    Size    Size                       `json:"size,omitempty"    yaml:"size,omitempty"`
    Cmd     string                     `json:"cmd,omitempty"     yaml:"cmd,omitempty"`
    Envs    []core.EnvironmentVariable `json:"envs,omitempty"    yaml:"envs,omitempty"`
    Patches []core.ServicePatch        `json:"patches,omitempty" yaml:"patches,omitempty"`
}

// GetID returns the ID of a reusable function.
func (o *ReusableFunctionDefinition) GetID() string {
    return o.ID
}

// GetType returns the Type of function.
func (o *ReusableFunctionDefinition) GetType() FunctionType {
    return ReusableContainerFunctionType
}

// Validate validates the reusable function definition.
func (o *ReusableFunctionDefinition) Validate() error {
    if o == nil {
        return nil
    }

    if o.ID == "" {
        return errors.New("id required")
    }

    if ok := utils.MatchesRegex(o.ID); !ok {
        return fmt.Errorf("function id must match regex: %s", utils.RegexPattern)
    }

    if o.Image == "" {
        return errors.New("image required")
    }

    return nil
}