preserve

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package preserve 实现 GB/T 42133—2022 第 6 章的长期保存处理。

该标准把条款分成两类:可机器判定的合规条件由 ofd-validator 按 profile 规则 上报;本质是转换动作的条款由本包执行。本包只产出新的字节流,不修改输入。

转换以 XML 树操作为主,改用 beevik/etree 对既有文档做定点编辑,实测在真实 样例上字节往返一致。

不用 internal/models 的原因是它的读路径无法安全回写:OFD.xml 的 xmlns 属性 在解码后不会落到模型的 XMLNS 字段(字段 tag 是 xmlns:ofd,与 Go 解码出的 属性名不匹配),因此「解析 → 序列化」会写出 <Document xmlns:ofd="">,命名 空间 URI 丢失,产出的不再是合法 OFD。该模型的 MarshalXML 系列方法已随之移除, 写回路径统一走 etree。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change struct {
	// Entry 是被改动的包内路径。
	Entry string
	// Clause 是依据条款。
	Clause string
	// Action 是人类可读的改动说明。
	Action string
	// Count 是该条款在本次转换中实际改动的节点数。
	Count int
}

Change 描述一处改动,供 --dry-run 展示与审计留痕。

func (Change) String

func (c Change) String() string

String 按单行输出,便于文本报告逐条列举。

type Options

type Options struct {
	// DocType 是 OFD profile 取值,空值时按文件声明的 DocType 自动判定。
	DocType string
	// Limits 限制输入与输出的规模,零值使用默认限制。
	Limits replace.Limits
	// Validate 在写出前确认转换没有引入新问题。
	//
	// 语义刻意不是「输出必须零错误」:真实 OFD 常带既有 XSD 偏差,若以零错误
	// 为门槛,转换会在多数文件上直接拒绝输出。判据是错误问题码集合不扩大,
	// 详见 verifyNoRegression。
	Validate bool
	// OnWarning 接收非致命提示,例如输入本就不合规、签名摘要已失效。
	OnWarning func(string)
	// DropUnreferenced 允许真的删除无人引用的条目(GB/T 42133 6.2.1 c))。
	//
	// 默认关闭,只报告不删除。删除不可逆,而闭包的正确性依赖引用识别:漏识别
	// 一种引用形式就会删掉在用文件。开启时若闭包不完整仍会拒绝删除。
	DropUnreferenced bool
}

Options 控制转换行为。

type Result

type Result struct {
	// DocType 是本次转换实际应用的 profile 取值。
	DocType string
	// Entry 是被改动的包内路径,无改动时为空。
	Entry string
	// Changes 是已做出的改动;Plan 给出的是计划中的改动。
	Changes []Change
	// Entries 是被改动的包内路径集合,无改动时为空。
	Entries []string
	// BeforeErrors 是转换前的错误数,Validate 为真时有效。
	BeforeErrors int
	// Unreferenced 是包内无人引用的条目,即 6.2.1 c) 的删除候选。
	Unreferenced []string
	// UnreferencedDropped 是实际被删除的条目数。
	UnreferencedDropped int
	// ClosureIncomplete 为真时闭包不可信,删除候选不得据此删除。
	ClosureIncomplete bool
	// ClosureReason 说明闭包为何不可信。
	ClosureReason string
}

Result 是一次转换的结果。

func Apply

func Apply(input any, w io.Writer, options Options) (Result, error)

Apply 执行转换并把结果写入 w。输出始终是新的字节流,输入不受影响。

全部校验与写出都成功后才真正写给 w,因此失败时 w 不会拿到半成品。

func Plan

func Plan(input any, options Options) (Result, error)

Plan 计算将要做出的改动,不写任何文件。--dry-run 走这条路径。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL