diff --git a/gen/nvml/generateapi.go b/gen/nvml/generateapi.go index 5ec4a07..9abfea0 100644 --- a/gen/nvml/generateapi.go +++ b/gen/nvml/generateapi.go @@ -77,6 +77,7 @@ var GeneratableInterfaces = []GeneratableInterfacePoperties{ { Type: "nvmlVgpuTypeId", Interface: "VgpuTypeId", + Exclude: []string{"ID"}, }, } @@ -239,7 +240,18 @@ func generateInterface(sourceDir string, input GeneratableInterfacePoperties) (s return "", err } + // We separate the methods into two sets to ensure that extensions are always included last. + var generatedMethods []*ast.FuncDecl + var extensions []*ast.FuncDecl for _, method := range methods { + if method.Name.Name == "Extensions" { + extensions = append(extensions, method) + continue + } + generatedMethods = append(generatedMethods, method) + } + + for _, method := range append(generatedMethods, extensions...) { formatted := fmt.Sprintf("\t%s\n", formatMethodSignature(method)) signature.WriteString(formatted) } diff --git a/pkg/nvml/api.go b/pkg/nvml/api.go index fdf27bd..b568474 100644 --- a/pkg/nvml/api.go +++ b/pkg/nvml/api.go @@ -27,6 +27,11 @@ type ExtendedInterface interface { LookupSymbol(string) error } +//go:generate moq -rm -out mock/extendedvgputypeid.go -pkg mock . ExtendedVgpuTypeId:ExtendedVgpuTypeId +type ExtendedVgpuTypeId interface { + ID() uint32 +} + // libraryOptions hold the paramaters than can be set by a LibraryOption type libraryOptions struct { path string diff --git a/pkg/nvml/mock/extendedvgputypeid.go b/pkg/nvml/mock/extendedvgputypeid.go new file mode 100644 index 0000000..9a230ef --- /dev/null +++ b/pkg/nvml/mock/extendedvgputypeid.go @@ -0,0 +1,68 @@ +// Code generated by moq; DO NOT EDIT. +// github.com/matryer/moq + +package mock + +import ( + "github.com/NVIDIA/go-nvml/pkg/nvml" + "sync" +) + +// Ensure, that ExtendedVgpuTypeId does implement nvml.ExtendedVgpuTypeId. +// If this is not the case, regenerate this file with moq. +var _ nvml.ExtendedVgpuTypeId = &ExtendedVgpuTypeId{} + +// ExtendedVgpuTypeId is a mock implementation of nvml.ExtendedVgpuTypeId. +// +// func TestSomethingThatUsesExtendedVgpuTypeId(t *testing.T) { +// +// // make and configure a mocked nvml.ExtendedVgpuTypeId +// mockedExtendedVgpuTypeId := &ExtendedVgpuTypeId{ +// IDFunc: func() uint32 { +// panic("mock out the ID method") +// }, +// } +// +// // use mockedExtendedVgpuTypeId in code that requires nvml.ExtendedVgpuTypeId +// // and then make assertions. +// +// } +type ExtendedVgpuTypeId struct { + // IDFunc mocks the ID method. + IDFunc func() uint32 + + // calls tracks calls to the methods. + calls struct { + // ID holds details about calls to the ID method. + ID []struct { + } + } + lockID sync.RWMutex +} + +// ID calls IDFunc. +func (mock *ExtendedVgpuTypeId) ID() uint32 { + if mock.IDFunc == nil { + panic("ExtendedVgpuTypeId.IDFunc: method is nil but ExtendedVgpuTypeId.ID was just called") + } + callInfo := struct { + }{} + mock.lockID.Lock() + mock.calls.ID = append(mock.calls.ID, callInfo) + mock.lockID.Unlock() + return mock.IDFunc() +} + +// IDCalls gets all the calls that were made to ID. +// Check the length with: +// +// len(mockedExtendedVgpuTypeId.IDCalls()) +func (mock *ExtendedVgpuTypeId) IDCalls() []struct { +} { + var calls []struct { + } + mock.lockID.RLock() + calls = mock.calls.ID + mock.lockID.RUnlock() + return calls +} diff --git a/pkg/nvml/vgpu_extensions.go b/pkg/nvml/vgpu_extensions.go new file mode 100644 index 0000000..ce55aa9 --- /dev/null +++ b/pkg/nvml/vgpu_extensions.go @@ -0,0 +1,27 @@ +// Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package nvml + +func (vgpuTypeId *nvmlVgpuTypeId) Extensions() ExtendedVgpuTypeId { + return vgpuTypeId +} + +// ID returns the numeric representaion of the vgpuTypeId. +func (vgpuTypeId *nvmlVgpuTypeId) ID() uint32 { + if vgpuTypeId == nil { + return 0 + } + return uint32(*vgpuTypeId) +} diff --git a/pkg/nvml/zz_generated.api.go b/pkg/nvml/zz_generated.api.go index c1ecb2d..7c42080 100644 --- a/pkg/nvml/zz_generated.api.go +++ b/pkg/nvml/zz_generated.api.go @@ -583,7 +583,6 @@ type Interface interface { EventSetCreate() (EventSet, Return) EventSetFree(EventSet) Return EventSetWait(EventSet, uint32) (EventData, Return) - Extensions() ExtendedInterface GetExcludedDeviceCount() (int, Return) GetExcludedDeviceInfoByIndex(int) (ExcludedDeviceInfo, Return) GetVgpuCompatibility(*VgpuMetadata, *VgpuPgpuMetadata) (VgpuPgpuCompatibility, Return) @@ -668,6 +667,7 @@ type Interface interface { VgpuTypeGetName(VgpuTypeId) (string, Return) VgpuTypeGetNumDisplayHeads(VgpuTypeId) (int, Return) VgpuTypeGetResolution(VgpuTypeId, int) (uint32, uint32, Return) + Extensions() ExtendedInterface } // Device represents the interface for the nvmlDevice type. @@ -1004,4 +1004,5 @@ type VgpuTypeId interface { GetNumDisplayHeads() (int, Return) GetResolution(int) (uint32, uint32, Return) GetSupportedPlacements(Device) (VgpuPlacementList, Return) + Extensions() ExtendedVgpuTypeId }