Files
s3/weed/s3api/iceberg/metadata_compliance.go
zooqueen dd74d3e6d6 Replace AGPL MinIO fork with Apache-2.0 SeaweedFS fork; rebrand CLI weed->s3
hanzoai/s3 was an AGPL-3.0 MinIO fork whose network-copyleft blocks embedding
and reselling as a managed service. Replace it wholesale with a fork of
SeaweedFS (github.com/seaweedfs/seaweedfs, Apache-2.0, Go) which ships a
built-in S3 gateway + Filer.

- License: AGPL-3.0 -> Apache-2.0 (upstream LICENSE kept verbatim, incl.
  "Copyright 2025 Chris Lu"); NOTICE added crediting SeaweedFS. No AGPL/BUSL/
  SSPL/GPL or enterprise ("ee") files remain.
- Module: go.mod path set to github.com/hanzoai/s3 with a self-replace
  (replace github.com/seaweedfs/seaweedfs => ./) so the ~1800 internal Go files
  keep their upstream import paths with zero churn. Internal identifiers and
  import paths are intentionally NOT renamed.
- CLI rebrand: the binary is built as `s3` (was `weed`); root usage, per-command
  help/usage/flag strings, shell autocomplete program name, and the product name
  in CLI output now read "Hanzo S3" / `s3`. Users never see `weed`.
  (e.g. `s3 server`, `s3 filer`, `s3 master`, `s3 volume`, `s3 s3`, `s3 mount`.)
- Build: `make` (or `go build -o s3 ./weed`) builds the `s3` binary green;
  `cmd/weed-db` and `cmd/weed-sql` also build; `go vet ./weed/command/` clean.
- README/LLM.md rewritten for Hanzo S3; AGENTS.md/CLAUDE.md symlinks preserved.
- Dropped a 33MB prebuilt Mach-O `stress` binary upstream had committed; `.gitignore`
  updated to ignore the `s3` build output.
2026-06-18 23:41:25 -07:00

133 lines
4.3 KiB
Go

package iceberg
import (
"bytes"
"encoding/json"
)
// specRequiredEmptyOrder is iterated in this fixed order so the byte-level
// append in ensureMetadataSpecCompliance produces deterministic output.
var specRequiredEmptyOrder = []string{
"current-snapshot-id",
"snapshots",
"snapshot-log",
"metadata-log",
"refs",
}
// specRequiredEmptyDefaults holds the sentinel values Iceberg requires when
// the corresponding state is empty.
var specRequiredEmptyDefaults = map[string]json.RawMessage{
"current-snapshot-id": json.RawMessage("-1"),
"snapshots": json.RawMessage("[]"),
"snapshot-log": json.RawMessage("[]"),
"metadata-log": json.RawMessage("[]"),
"refs": json.RawMessage("{}"),
}
// isJSONNull reports whether raw encodes the JSON null literal, ignoring
// insignificant whitespace allowed by RFC 8259.
func isJSONNull(raw json.RawMessage) bool {
return bytes.Equal(bytes.TrimSpace(raw), []byte("null"))
}
// ensureMetadataSpecCompliance backfills Iceberg spec-required fields that
// iceberg-go v0.5.0 drops via `omitempty` on optional pointer/slice fields.
//
// For tables with no snapshots (e.g. freshly created tables), iceberg-go
// represents an empty state with nil/empty Go values, then strips those keys
// entirely from the JSON output. The Iceberg REST spec, however, requires
// these keys to be present even when empty — Java/Spark/Trino clients
// fail to parse responses missing them (most visibly with
// "Cannot parse missing long current-snapshot-id"). This helper rehydrates
// the missing keys without overwriting any present real values.
//
// Missing keys are spliced in at the byte level just before the closing
// brace so iceberg-go's struct-declared key order is preserved (a naive
// map remarshal would alphabetize every key in the document). The slower
// remarshal path is only used when an explicit JSON null needs replacing,
// which iceberg-go itself never emits.
//
// Returns the original bytes unchanged when parsing fails or no key needs
// to be added, to avoid corrupting an otherwise-valid payload.
func ensureMetadataSpecCompliance(raw []byte) []byte {
if len(raw) == 0 {
return raw
}
var obj map[string]json.RawMessage
if err := json.Unmarshal(raw, &obj); err != nil {
return raw
}
// A top-level JSON null leaves obj == nil with no error; writes would
// panic on the slow path. It is not a valid TableMetadata document, so
// pass it through unchanged.
if obj == nil {
return raw
}
// A field is "missing" for spec purposes if it's absent OR encoded as
// JSON null. Some writers emit `"current-snapshot-id": null` for empty
// state instead of omitting; strict clients reject both the same way.
var toAppend []string
hasExplicitNull := false
for _, key := range specRequiredEmptyOrder {
v, present := obj[key]
switch {
case !present:
toAppend = append(toAppend, key)
case isJSONNull(v):
hasExplicitNull = true
}
}
if len(toAppend) == 0 && !hasExplicitNull {
return raw
}
if !hasExplicitNull {
return appendMissingObjectKeys(raw, toAppend, len(obj) > 0)
}
// Slow path: replace explicit nulls. Rare in practice, so the
// alphabetical key reordering inherent to map remarshal is acceptable.
for key, v := range obj {
if _, required := specRequiredEmptyDefaults[key]; required && isJSONNull(v) {
obj[key] = specRequiredEmptyDefaults[key]
}
}
for _, key := range toAppend {
obj[key] = specRequiredEmptyDefaults[key]
}
fixed, err := json.Marshal(obj)
if err != nil {
return raw
}
return fixed
}
// appendMissingObjectKeys splices the given keys into raw just before the
// closing brace of the top-level JSON object. raw is assumed to be a valid
// JSON object (the caller has already unmarshalled it). hasMembers tells us
// whether the object had any existing members, so we know whether to emit a
// leading comma for the first appended key.
func appendMissingObjectKeys(raw []byte, keys []string, hasMembers bool) []byte {
closeIdx := bytes.LastIndexByte(raw, '}')
if closeIdx < 0 {
return raw
}
var buf bytes.Buffer
buf.Grow(len(raw) + len(keys)*48)
buf.Write(raw[:closeIdx])
for _, key := range keys {
if hasMembers {
buf.WriteByte(',')
}
buf.WriteByte('"')
buf.WriteString(key)
buf.WriteString(`":`)
buf.Write(specRequiredEmptyDefaults[key])
hasMembers = true
}
buf.Write(raw[closeIdx:])
return buf.Bytes()
}