pulumi/pkg/diag/diag.go
2017-05-18 14:51:52 -07:00

66 lines
2 KiB
Go

// Licensed to Pulumi Corporation ("Pulumi") under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// Pulumi licenses this file to You 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 diag
import (
"fmt"
)
// ID is a unique diagnostics identifier.
type ID int
// Diag is an instance of an error or warning generated by the compiler.
type Diag struct {
ID ID // a unique identifier for this diagnostic.
Message string // a human-friendly message for this diagnostic.
Doc *Document // the document in which this diagnostic occurred.
Loc *Location // the document location at which this diagnostic occurred.
}
// Message returns an anonymous diagnostic message without any source or ID information.
func Message(msg string, args ...interface{}) *Diag {
return &Diag{Message: fmt.Sprintf(msg, args...)}
}
// Diagable can be used to determine a diagnostic's position.
type Diagable interface {
Where() (*Document, *Location)
}
// At adds a position to an existing diagnostic, retaining its ID and message.
func (diag *Diag) At(d Diagable) *Diag {
var doc *Document
var loc *Location
if d != nil {
doc, loc = d.Where()
}
return &Diag{
ID: diag.ID,
Message: diag.Message,
Doc: doc,
Loc: loc,
}
}
// AtFile adds a file to an existing diagnostic, retaining its ID and message.
func (diag *Diag) AtFile(file string) *Diag {
return &Diag{
ID: diag.ID,
Message: diag.Message,
Doc: NewDocument(file),
}
}