Skip to content

Commit

Permalink
Add conversion convenience functions for CvParam and UserParam
Browse files Browse the repository at this point in the history
  • Loading branch information
kMutagene committed Aug 17, 2023
1 parent 8ecd66b commit ccb95fd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
23 changes: 9 additions & 14 deletions src/ControlledVocabulary/CvParam.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ type CvParam(cvAccession : string, cvName : string, cvRef : string, paramValue :
member this.RefUri = cvRef
member this.Value = paramValue
member this.WithValue(v : ParamValue) = CvParam(cvAccession,cvName,cvRef,v,attributes)
member this.HasAttributes
with get() = this.Attributes |> Seq.isEmpty |> not

interface IParam with
member this.Accession = this.Accession
member this.Name = this.Name
member this.RefUri = this.RefUri
member this.Value = this.Value
member this.WithValue(v : ParamValue) = CvParam(cvAccession,cvName,cvRef,v,attributes)
member this.HasAttributes
with get() = this.Attributes |> Seq.isEmpty |> not
member this.Accession = this.Accession
member this.Name = this.Name
member this.RefUri = this.RefUri
member this.Value = this.Value
member this.WithValue(v : ParamValue) = this.WithValue(v)
member this.HasAttributes = this.HasAttributes

new (id,name,ref,pv,attributes : seq<IParam>) =
let dict = CvAttributeCollection(attributes)
Expand Down Expand Up @@ -127,12 +128,6 @@ type CvParam(cvAccession : string, cvName : string, cvRef : string, paramValue :
/// Returns true, if the names of the given param items match
static member equalsName (cvp1 : CvParam) (cvp2 : CvParam) = Param.equalsName cvp1 cvp1

/// Returns Some Value of type 'T, if the given param item can be downcast, else returns None
static member inline tryAs<'T when 'T :> IParam> (cvp: CvParam) = Param.tryAs<'T> cvp

/// Returns true, if the given param item can be downcast
static member inline is<'T when 'T :> IParam> (cvp: CvParam) = Param.is<'T> cvp

//---------------------- CvParam specific implementations ----------------------//

/// Create a CvParam from a category and a simple value
Expand All @@ -151,7 +146,7 @@ type CvParam(cvAccession : string, cvName : string, cvRef : string, paramValue :
param.Values |> Seq.cast

override this.ToString() =
$"CvParam: {(this :> ICvBase).Name}\n\tID: {(this :> ICvBase).Accession}\n\tRefUri: {(this :> ICvBase).RefUri}\n\tValue: {(this :> IParamBase).Value}\n\tAttributes: {this.Keys |> Seq.toList}"
$"CvParam: {this.Name}\n\tID: {this.Accession}\n\tRefUri: {this.RefUri}\n\tValue: {this.Value}\n\tAttributes: {this.Keys |> Seq.toList}"

member this.DisplayText =
this.ToString()
Expand Down
40 changes: 28 additions & 12 deletions src/ControlledVocabulary/UserParam.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ type UserParam(name : string, paramValue : ParamValue, attributes : IDictionary<
member this.Name = name
member this.RefUri = "UserTerm"
member this.Value = paramValue
member this.WithValue(v : ParamValue) = UserParam(name,v,attributes)
member this.HasAttributes
with get() = this.Attributes |> Seq.isEmpty |> not

interface IParam with
member this.Accession = this.Accession
member this.Name = this.Name
member this.RefUri = this.RefUri
member this.Value = this.Value
member this.WithValue(v : ParamValue) = UserParam(name,v,attributes)
member this.HasAttributes
with get() = this.Attributes |> Seq.isEmpty |> not
member this.Accession = this.Accession
member this.Name = this.Name
member this.RefUri = this.RefUri
member this.Value = this.Value
member this.WithValue(v : ParamValue) = this.WithValue(v)
member this.HasAttributes = this.HasAttributes

new (name,pv,attributes : seq<IParam>) =
let dict = CvAttributeCollection(attributes)
Expand Down Expand Up @@ -103,21 +105,23 @@ type UserParam(name : string, paramValue : ParamValue, attributes : IDictionary<
/// Returns true, if the names of the given param items match
static member equalsName (up1: UserParam) (up2: UserParam) = Param.equalsName up1 up2

/// Returns Some Value of type 'T, if the given param item can be downcast, else returns None
static member inline tryAs<'T when 'T :> IParam> (up: UserParam) = Param.tryAs<'T> up
//---------------------- UserParam specific implementations ----------------------//

/// Returns true, if the given param item can be downcast
static member inline is<'T when 'T :> IParam> (up: UserParam) = Param.is<'T> up
static member toCvParam (up: UserParam) = CvParam(CvTerm.create(up.Accession, up.Name, up.RefUri), up.Value, up.Attributes)

override this.ToString() =
$"Name: {(this :> ICvBase).Name}\n\tValue: {(this :> IParamBase).Value}\n\tQualifiers: {this.Keys |> Seq.toList}"
$"Name: {this.Name}\n\tValue: {this.Value}\n\tQualifiers: {this.Keys |> Seq.toList}"

member this.DisplayText =
this.ToString()

[<AutoOpen>]
module UserParamExtensions =

type CvParam with
static member toUserParam (cvp: CvParam) =
UserParam(cvp.Name, cvp.Value, cvp.Attributes)

type ParamBase with
/// Returns Some Param if the given value item can be downcast, else returns None
static member tryUserParam (cv : IParamBase) =
Expand All @@ -132,6 +136,18 @@ module UserParamExtensions =
| :? UserParam as param -> Some param
| _ -> None

static member toCvParam (cv : IParam) =
match cv with
| :? UserParam as up -> up |> UserParam.toCvParam
| :? CvParam as cvp -> cvp
| _ -> failwith "no conversion to CvParam available for this type"

static member toUserParam (cv : IParam) =
match cv with
| :? UserParam as up -> up
| :? CvParam as cvp -> cvp |> CvParam.toUserParam
| _ -> failwith "no conversion to CvParam available for this type"

type CvBase with
/// Returns Some UserParam, if the given value item can be downcast, else returns None
static member tryUserParam (cv : ICvBase) =
Expand Down

0 comments on commit ccb95fd

Please sign in to comment.